Limit in include?

I have a page where i go through all categories, and show the latest 3 games for each one. Right now i include those games when i fetch the categories in the controller:

@categories = Category.find :all, :include => [:online_games]

Is there a way to limit those games here to the amount that i want? It seems a bit inefficient to get all games per category here, and limit those in the view.

Peter wrote:

I have a page where i go through all categories, and show the latest 3 games for each one. Right now i include those games when i fetch the categories in the controller:

@categories = Category.find :all, :include => [:online_games]

Is there a way to limit those games here to the amount that i want? It seems a bit inefficient to get all games per category here, and limit those in the view.

Why would not :limit => 3 work?

because that would limit the categories to 3, not the games.

Peter wrote:

because that would limit the categories to 3, not the games.

On Mar 17, 4:08 pm, James Byrne <rails-mailing-l...@andreas-s.net>

more than what you are asking, you can make it the default :limit option when eager loading associations.

i searched for 'eager loading associations ruby rails' in google: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html in this page, search for 'limit'.

you can add a :limit => n number to the declaration in the model (has_many .. :limit => ) or whatever as the default limitation for the association you want to query.

hope it helps out :slight_smile:

shai