More inverse relationships defined

This commit is contained in:
Brenda Wallace
2018-10-16 13:20:03 +13:00
parent 0d3588a2b2
commit de2175ce16
4 changed files with 12 additions and 7 deletions

View File

@@ -62,7 +62,7 @@ class Garden < ApplicationRecord
unique_plantings = []
seen_crops = []
plantings.order(created_at: :desc).includes(:garden, :crop, :owner, :harvests).each do |p|
plantings.includes(:garden, :crop, :owner, :harvests).order(created_at: :desc).each do |p|
unless seen_crops.include?(p.crop)
unique_plantings.push(p)
seen_crops.push(p.crop)

View File

@@ -3,7 +3,8 @@ class Photo < ApplicationRecord
PHOTO_CAPABLE = %w(Garden Planting Harvest Seed).freeze
has_many :photographings, foreign_key: :photo_id, dependent: :destroy
has_many :photographings, foreign_key: :photo_id, dependent: :destroy, inverse_of: :photo
# creates a relationship for each assignee type
PHOTO_CAPABLE.each do |type|
has_many type.downcase.pluralize.to_s.to_sym,

View File

@@ -1,5 +1,5 @@
class Photographing < ApplicationRecord
belongs_to :photo
belongs_to :photo, inverse_of: :photographings
belongs_to :photographable, polymorphic: true
validate :photo_and_item_have_same_owner

View File

@@ -21,10 +21,14 @@ class Planting < ApplicationRecord
#
# Ancestry of food
belongs_to :parent_seed, class_name: 'Seed', foreign_key: 'parent_seed_id',
required: false # parent
has_many :child_seeds, class_name: 'Seed',
foreign_key: 'parent_planting_id', dependent: :nullify # children
belongs_to :parent_seed, class_name: 'Seed', # parent
foreign_key: 'parent_seed_id',
required: false,
inverse_of: :child_plantings
has_many :child_seeds, class_name: 'Seed', # children
foreign_key: 'parent_planting_id',
inverse_of: :parent_planting,
dependent: :nullify
##
## Scopes