On 16/04/11 11:18 AM, Chris Bloom wrote:
I have a legacy database that I just need to write a report off of,
basically looking for duplicate entries. The schema is basically like
this:
class Profile
has n, :registrations
end
class Registration
belongs_to :profile
has n, :signups
has n, :payments
end
class Signup
belongs_to :registration
belongs_to :event
has n, :payments
end
class Payment
belongs_to :registration
belongs_to :signup
end
class Event
has n, :signups
has n, :payments, :through => :signups
end
I just want to print out a table of all of the profiles, each with
it's registrations, each with it's event signups, each with it's
payments. The problem is that it is resulting in (n+1)*(n+1)*(n+1)*(n
+1) queries. Is there a way to force DataMapper to do eager loading of
these nested models right from the start? Is there some pre-loading
that would make more sense?
One way that is a bit messy but I have used in the past is to do the
eager loading yourself, which gets the records in the identity map so
they will used deeper down the chain when an N+1 would be triggered.
events = Event.all
Signup.all(:event_id => events.map(&:id)).to_a # to_a is required to
trigger the load
# etc...
FWIW, I'm new to both Ruby and DataMapper.
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.