2011-05-07 38 views
0

我在我的rails3应用程序中设置了mongoid,并创建了2个模型。 一个模型是用户,另一个模型是文章。我如何公开一个Mongoid嵌入式集合?

既然每个用户都可以创建很多文章,我已经把:

embedded_in :user 
模型/ article.rb文件

,并:

embeds_many :articles 

模型/ 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. 

虽然我想保持关系,但我希望文章能够被任何人访问。我怎样才能做到这一点??

回答

1

另外,如果你真的需要做嵌入式的文章,这样做:

User.where("article.id" => params[:id].first.articles.find(params[:id]) 

但是,正如Ben所说,你最好使用belongs_to而不是embedded_in。