2011-05-07 46 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。

相關問題