I have mongoid setup in my rails3 app, and have created 2 models. One model is user, and the other mod开发者_开发技巧el 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??
It sounds like what you want is a referenced relation rather than an embeds relation for this: http://mongoid.org/docs/relations/referenced.html
also, if you really need to make articles embedded, do this:
User.where("article.id" => params[:id].first.articles.find(params[:id])
but, as Ben said, you would better use belongs_to instead of embedded_in.
精彩评论