ruby on rails - Mongoid : Eager loading count on has_many association -


i'm new mongodb.

i'm trying list documents, class has has_many association, display number of documents association.

do need eager loading ?

if use includes documents loaded, want count.

identity map enabled

i don't know of way prevent eager loading loading full document. i'd curious hear if that's possible now.

you can count this:

childdoc.where(myclass_id: myclass_obj.id).count 

so you're querying on implicitly created foreign key field of association. still query per parent doc, ideally fast query.

if perf real concern you, write single query return more data in single trip -- ids of contained docs -- this:

childdoc.where(myclass_id: {"$in" => list_of_myclass_objs.map {|x| x.id}}).only(:id, :myclass_id) 

since you're new this, i'll add want create index on fk field -- mongoid doesn't you.

class childdoc     include mongoid::document     belongs_to :myclass     index({ myclass_id: 1 }) end 

and then

rake db:mongoid:create_indexes 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -