Question

I have a following association

Class Person 
    include Mongoid::Document
    embeds_many :employments
end

Class Employment
    include Mongoid::Document
    references_many :centres
end

class Centre
    include Mongoid::Document
    referenced_in :employment
end

Now when I tried

Person.first.employments.first.centres.build it gave me errors like

NoMethodError: undefined method `centres' for #<Employment:0x000001023f38f8>

Am i doing any thing wrong?

Or the embedded document cannot reference many other documents?

Was it helpful?

Solution

Dude, you setup is wrong. Embedded document cannot reference other model. If you still want to reference another model with embedded document, then you will have to create custom function.

OTHER TIPS

Try:

class Centre
  include Mongoid::Document
  referenced_in :employment, :inverse_of => :centres
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top