ruby - Mongoid: Referencing Same Model More Than Once Through has_many -
i'd able reference model (a has_many relationship) more once in same model. example, given following models:
class mymodel include mongoid::document field :name, type: string has_many :main_efforts, :class_name => 'effort', as: :effortable, dependent: :delete, autosave: true has_many :secondary_efforts, :class_name => 'effort', as: :effortable, dependent: :delete, autosave: true validates_presence_of :name end class effort include mongoid::document field :name, type: string belongs_to :effortable, polymorphic: true validates_presence_of :name end
as can see, effort model referenced twice. originally, effort model wasn't polymorphic, seemed mongoid unable determine collection (main_efforts or secondary_efforts) effort belonged to. such, made polymorphic. after making polymorphic, however, main_efforts , secondary_efforts fields empty array.
what proper way reference polymorphic model more once in same model (assuming polymorphic model necessary)?
figured out:
class mymodel include mongoid::document field :name, type: string has_many :main_efforts, :class_name => 'effort', dependent: :delete, autosave: true, :inverse_of => :main_effort has_many :secondary_efforts, :class_name => 'effort', dependent: :delete, autosave: true, :inverse_of => :secondary_effort validates_presence_of :name end class effort include mongoid::document field :name, type: string belongs_to :main_effort, :class_name => 'conop', :inverse_of => :main_efforts belongs_to :secondary_effort, :class_name => 'conop', :inverse_of => :secondary_efforts validates_presence_of :name end
Comments
Post a Comment