# Limit in include?

**URL:** <https://www.ruby-forum.com/t/limit-in-include/132165>\
**Category:** Rails\
**Created:** [March 17, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/limit-in-include/132165 "2008-03-17T15:35:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 17, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/limit-in-include/132165/1 "2008-03-17T15:35:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 17, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/limit-in-include/132165/2 "2008-03-17T16:08:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 17, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/limit-in-include/132165/3 "2008-03-17T16:29:00Z")

</div>

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

On Mar 17, 4:08 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 17, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/limit-in-include/132165/4 "2008-03-17T16:35:31Z")

</div>

Peter wrote:

> because that would limit the categories to 3, not the games.
> 
> On Mar 17, 4:08 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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](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 🙂

shai
