2010-11-22 31 views
0

說我有以下對象:調用find(:所有)從實例對象Mongoid

{ "text" : "oa3", "topic_ids" : [ ObjectId("4cea00efd8030a35eb000004") ]} 

我代表這個名爲「一個」

a.topics.find(:all).count #this returns 0 

我覺得一個對象,我我做錯了。

如何檢索此特定對象中主題的迭代器?

回答

4
# get the number of topics 
a.topics.count 

# same but faster 
a.topic_ids.count 

# get an array of the topics 
a.topics.entries 

# do a query on the topics 
a.topics.where(:title => 'Movies').entries 

的關鍵是使用Mongoid的標準(Model.whereModel.association.where)做查詢,而不是ActiveRecord的風格發現者(Model.find)。 ActiveRecord風格的發現者實際上只是爲了方便 - Mongoid的真正威力在Criteria中。在Mongoid網站

更多信息:

http://mongoid.org/docs/querying/

相關問題