-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
class User include Mongoid::Document has_many :images end class Image include Mongoid::Document belongs_to :user end
Then eager loading the user off of the image....
user = User.create Image.create(:user => user) image = Image.includes(:user).first # MOPED: ... database=development collection=images selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (3.6521ms) # MOPED: ... database=development collection=users selector={"_id"=>{"$in"=>["514cb621f433ea9332000001"]}} flags=[] limit=0 skip=0 batch_size=nil fields=nil (0.2980ms) image.user # MOPED: ... database=development collection=users selector={"$query"=>{"_id"=>"514cb621f433ea9332000001"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (0.4160ms)
As you can see the user is queried for off the image load but is then queried for when you actually reference the user.
# Gemfile
GIT
remote: git://github.com/mongoid/mongoid.git
revision: de244e9c323d777cdf6c260e9461200ad932185b
specs:
mongoid (4.0.0)
activemodel (~> 4.0.0.beta)
moped (~> 1.4.2)
origin (~> 1.0)
tzinfo (~> 0.3.22)