Eager Loading DataMapper Associations

386 views
Skip to first unread message

Dan Kubb (dkubb)

unread,
Jul 22, 2012, 1:49:51 PM7/22/12
to datam...@googlegroups.com
Hi Everyone,

I just figured I'd share this with the list, it's a way of Eager Loading DM associations by specifying what associations to load:


I'll probably gemify it soon, but I wanted to get it out early in case people want to hack on it and test it out.

-- 

Dan

Till Schulte-Coerne

unread,
Jul 23, 2012, 3:39:15 AM7/23/12
to datam...@googlegroups.com
Hi Dan,

thanks for sharing this.

There was a discussion about eager loading a while ago: https://gist.github.com/1244491

We took that code (provided by d11wtq), changed some details and ended up here:

I guess hacking in the model's instance variables wasn't the best idea. But I really like the API (using the QueryPath and a method in DataMapper::Collection).

--

Till


--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/O0xqTr-P0XIJ.
To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.

Dan Kubb (dkubb)

unread,
Jul 24, 2012, 12:24:21 AM7/24/12
to datam...@googlegroups.com
Hi Till,


thanks for sharing this.

There was a discussion about eager loading a while ago: https://gist.github.com/1244491

We took that code (provided by d11wtq), changed some details and ended up here:
 
I guess hacking in the model's instance variables wasn't the best idea. But I really like the API (using the QueryPath and a method in DataMapper::Collection).

That's a beautiful API. I took those ideas and combined them with mine, and ended up improving my gist:


The one takes one or more query paths as arguments, and eager loads all the objects along each path. This is exactly what I was hoping to do, but I didn't expect it to be so simple ;)

My next step will be to make something that extends DataMapper::Collection with a method that wraps things up into something more convenient. I'm not sure yet if I'll name the method #preload, #eager_load or something else.

-- 

Dan

Avdi Grimm

unread,
Jul 31, 2012, 2:03:04 AM7/31/12
to datam...@googlegroups.com
Unrelated, but this reminds me... are there known issues with STI and lazy loading? I had an issue the other day where all the "child" attributes on STI subclasses came up as nil when used. Changing the iteration from:

sti_collection.each do |record|
  ...
end

to:

sti_collection.to_a.each do |record|
   ...
end

fixed it.


-- 

Dan

--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/O0xqTr-P0XIJ.
To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.



--
Avdi Grimm
http://avdi.org

I only check email twice a day. to reach me sooner, go to http://awayfind.com/avdi
Reply all
Reply to author
Forward
0 new messages