如何公开 Mongoid 嵌入式集合?
我在 Rails3 应用程序中设置了 mongoid,并创建了 2 个模型。 一种模型是用户,另一种模型是文章。
由于我每个用户都可以创建许多文章,因此我将:
embedded_in :user
放在 model/article.rb 文件中,并且:
embeds_many :articles
放在 model/user.rb 文件中。
现在,如果我通过“app_url/articles/random_article_id”访问文章,则会收到以下错误。
Access to the collection for Article is not allowed since it is an embedded document, please access a collection from the root document.
虽然我想维持关系,但我希望任何人都可以访问文章。我怎样才能做到这一点?
I have mongoid setup in my rails3 app, and have created 2 models.
One model is user, and the other model is article.
Since I each user can create many articles, I have put:
embedded_in :user
in model/article.rb file, and:
embeds_many :articles
in model/user.rb file.
Now, if I access article by 'app_url/articles/random_article_id' I get the following error.
Access to the collection for Article is not allowed since it is an embedded document, please access a collection from the root document.
While I want to maintain relationship, I want articles to be accessible to any people. How can I do that??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想要的是引用关系而不是嵌入关系: http://mongoid .org/docs/relations/referenced.html
It sounds like what you want is a referenced relation rather than an embeds relation for this: http://mongoid.org/docs/relations/referenced.html
另外,如果您确实需要嵌入文章,请执行以下操作:
但是,正如Ben所说,您最好使用belongs_to而不是embedded_in。
also, if you really need to make articles embedded, do this:
but, as Ben said, you would better use belongs_to instead of embedded_in.