lazy loading associations?

19 views
Skip to first unread message

Michael Bulat

unread,
May 30, 2008, 4:51:01 PM5/30/08
to rubyamf
I'm using rubyamf for some simple datagrid components, and as such, I
am not creating any custom class mappings or VOs. At the moment, I am
doing the following in my controller:

format.amf { render :amf => @projects }

This works wonderfully. However, I've got quite a few has_many
associations on my projects model, which significantly increases the
response time. Is there any way to lazy load these associations (i.e.
only load the associated ids)? I'm using amf instead of xml here,
because I want as little overhead as possible, however, without the
ability to lazy load, the response overhead goes up considerably.

Perhaps this functionality already exists, and I'm missing it. If not,
I'm thinking it would be helpful to have an option that would eager
load requested associations, similar the standard active record eager
loading option, for example:

format.amf { render :amf =>
@projects.to_amf(:include=> :notes) }

Luke Pillow

unread,
May 30, 2008, 7:56:45 PM5/30/08
to rub...@googlegroups.com
I want to make sure I understand what you'd like to happen...

You've got the following classes:

   class Project < ActiveRecord::Base
     has_many :notes
   end

   class Note < ActiveRecord::Base
     belongs_to :project
   end

Right?

When you respond_to
   format.amf { render :amf => @projects }
you want the result you receive in ActionScript to be an Array of projects with each project (being an ObjectProxy since you're not using VOs) having a notes property that is an Array of ids - othe primary key of each child note?  You don't want that notes property to be an Array of the actual note ObjectProxy representation of the ActiveRecord instances?

If this is correct, how would you forsee yourself using the collection of ids once having them in your ActionScript code?

Please correct any of my misunderstandings.  I really want to make sure I understand the end result that you're seeking.

Michael Bulat

unread,
May 31, 2008, 10:32:16 AM5/31/08
to rubyamf
Yes, you've pretty much got it. Though I realize I wasn't very clear
about what I meant about loading the ids.

What I meant to suggest was that rubyamf would lazy load associations
the same way activerecord lazy loads associations. So for each
belongs_to association, rubyamf would only get the id for the
associated object. For has_many associations, rubyamf would not load
any data, unless specified with an :include option.

Also the eager loading performed by rubyamf results in lots of dreaded
1+N queries. If I do the following in my controller:

@projects = Project.find(:all, :include => :notes)

Rails generates one query it titles "Project Load Including
Associations" in the debug output. The single sql query has JOIN on
the notes table. If I then follow this eager loading with

format.xml { render :xml => @projects.to_xml(:include => :notes) }

No additional queries are run, and the notes are part of the XML
output. However if I do the following:

format.amf { render :amf => @projects }

I get additional queries run for every single notes association (even
though I've already eager loaded the data). I'm assuming this has
something to do with how rubyamf is serializing the data. If there's
an option to get rubyamf to use the eager loaded data through the 1
JOIN query, then I might not even need the lazy load option, since it
seems the bigger bottleneck is the performance of all the 1+N
queries.

Michael Bulat

unread,
May 31, 2008, 1:27:09 PM5/31/08
to rubyamf
I think I just managed to answer my own question by digging around
some more.

I'd migrated over my code base from rails 1.2, and as such I still
have many of my views as .rhtml files. Apparently what's been
happening is that rails has been executing the rhtml files with the
amf requests. My old unused views for the project where pulling in all
the associations without any eager loading, hence all the extra 1+N
queries.

So renaming my index view to index.html.erb has solved the problem.
Now only a single query is run, and I can choose to eager load
associations or not as usual.

Sorry for the run around on this.
Reply all
Reply to author
Forward
0 new messages