user482594
user482594

Reputation: 17486

How can I expose a Mongoid embedded collection?

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??

Upvotes: 0

Views: 332

Answers (2)

sandrew
sandrew

Reputation: 3129

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.

Upvotes: 1

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

It sounds like what you want is a referenced relation rather than an embeds relation for this: http://mongoid.org/docs/relations/referenced.html

Upvotes: 1

Related Questions