2010-11-22 3 views
0
의 인스턴스 객체로부터는

난 다음 개체를 가지고 말 :호출 찾기 (모든) Mongoid

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

나는 이것이 소위 "는"

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

내가 느끼는 나타내는 개체가 그 I 이 잘못하고있다.

이 특정 개체의 항목에 대한 반복기는 어떻게 검색합니까?

답변

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 

의 핵심은 쿼리 대신 액티브 스타일 측정기 (Model.find를) 할 Mongoid의 기준 (Model.where 또는 Model.association.where)를 사용하는 것입니다. ActiveRecord 스타일 파인더는 Mongoid의 진정한 힘은 Criteria에있다. Mongoid 사이트에서

상세 정보 :

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