-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
Mongoid version 3.1.4.
This is a continuation to Issue MONGOID-2916
The same case doesn't work if the included model is paranoid, so:
class Band
include Mongoid::Document
has_many :albums
end
class Album
include Mongoid::Document
include Mongoid::Paranoia
belongs_to :band
field :name
end
Album.create(band_id: Band.create.id)
Album.create(band_id: Band.create.id)
And then did this with logging:
Band.includes(:albums).each do |band|
p band.albums.first.name # Does not hit the database again.
end
MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=bands selector={} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.6180ms)
MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=albums selector={"deleted_at"=>nil, "band_id"=>{"$in"=>["519a1df35b9e05cc01000001", "519a1df35b9e05cc01000003"]}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.6425ms)
MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=albums selector={"$query"=>
, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.6695ms)
MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=albums selector={"$query"=>
, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.6542ms)
So it does hit the database again.