开发者

Calling find(:all) from an instance object in Mongoid

Say i have the following object:

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

I have a object representing this called "a"

a.topics.find(:a开发者_高级运维ll).count  #this returns 0

I feel that I am doing this wrong.

How do I retrieve the iterator for topics in this particular object?


# 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

The key is to use Mongoid's Criteria (Model.where or Model.association.where) to do queries instead of the ActiveRecord style finders (Model.find). The ActiveRecord-style finders are really just for convenience--the real power of Mongoid is in Criteria.

More info on the Mongoid site:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜