开发者

about embedded document referencing many other documents

开发者 https://www.devze.com 2023-01-18 14:04 出处:网络
I have a following association Class Person include Mongoid::Document embeds_m开发者_如何学运维any :employments

I have a following association

Class Person 
    include Mongoid::Document
    embeds_m开发者_如何学运维any :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?


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.


Try:

class Centre
  include Mongoid::Document
  referenced_in :employment, :inverse_of => :centres
end
0

精彩评论

暂无评论...
验证码 换一张
取 消