Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do eager loading with limits?

In the documentation for eager loading it is stated that:


If you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects:

class Picture < ActiveRecord::Base
  has_many :most_recent_comments, :class_name => 'Comment', 
                                  :order => 'id DESC', :limit => 10
end

Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments.


If this is the case then what is the best way to achieve the "limit" on the loading?

Let's say we're eager loading the last 10 blog posts onto the front page of a blog, we clearly don't want them all so should the limit and ordering of the post collection be specified?

Further to that, can one specify the same conditions on elements that are deep loaded - for instance only show the first three comments on each blog post?

Blog.find(:blog_id, :include => {:posts => :comments } )
like image 504
Peter Nixey Avatar asked Jun 25 '11 12:06

Peter Nixey


1 Answers

I believe this is because the LIMIT command in sql doesn't translate well to what you are trying to do. LIMIT will limit the total rows returned by the query. You aren't trying to do that though. You are trying to limit the number of rows joined for each picture returned. In order to achieve this affect you'd have to use some complex SQL, which might be hard to optimize if your tables are large. At that point I would consider why you are trying to limit the rows eager loaded.

If the max number of comments eager loaded is manageable (< 2000 or so), you should probably not worry about limiting on the SQL end.

If you're only loading 10 posts though, I would consider not eager loading at all. I wouldn't expect an extra 10 queries to slow things down much, and what time they did add, you could make up for by trying other optimization techniques such as caching.

You should let scopes do the dirty work for you, not the assocation. This promotes reusability, maintainability, readability. Example:

Class Picture < ActiveRecord::Base
  has_many :comments, :order => 'id DESC' do
    def recent
      limit(10)
    end
  end
end

That way .comments is there when you need it, and you can also scope it down like this:

@picture.comments.recent
like image 80
Shane Avatar answered Nov 04 '22 17:11

Shane