2010-11-22 2 views
0

sagen, ich habe folgendes Objekt:Aufruf find (: alle) von einem Instanz-Objekt in Mongoid

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

Ich habe ein Objekt darstellt dies als "a"

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

Ich fühle, dass ich mache ich das falsch.

Wie lade ich den Iterator für Themen in diesem bestimmten Objekt?

Antwort

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 

Der Schlüssel ist Mongoid Criteria (Model.where oder Model.association.where) zu tun Anfragen anstelle der Active Stil Findern (Model.find) zu verwenden. Die ActiveRecord-Stil Finder sind wirklich nur für die Bequemlichkeit - die wahre Macht von Mongoid ist in Kriterien.

Mehr Infos auf der Website Mongoid:

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