关于引用许多其他文档的嵌入文档

发布于 2024-09-25 20:21:40 字数 523 浏览 7 评论 0原文

我有一个以下关联

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

现在,当我尝试

Person.first.employments.first.centres.build 时,它给了我错误,例如

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

我做错了什么吗?

或者嵌入文档无法引用许多其他文档?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜血染红嫁衣 2024-10-02 20:21:40

兄弟,你设置错了。嵌入文档不能引用其他模型。如果您仍然想引用带有嵌入文档的另一个模型,那么您将必须创建自定义函数。

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.

旧人哭 2024-10-02 20:21:40

尝试:

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

Try:

class Centre
  include Mongoid::Document
  referenced_in :employment, :inverse_of => :centres
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文