# Limit on eager loaded association?

**URL:** <https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256>\
**Category:** Rails\
**Created:** [May 16, 2007, 12:03am UTC](https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256 "2007-05-16T00:03:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [May 16, 2007, 12:03am UTC](https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256/1 "2007-05-16T00:03:24Z")

</div>

If I use :include to eager load a has\_many association, can I limit the  
number returned on that has\_many association. I know I can limit the  
amount of records returned, but how do I limit how many are returned on  
its association?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [May 16, 2007, 1:58am UTC](https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256/2 "2007-05-16T01:58:07Z")

</div>

Hi,

> If I use :include to eager load a has\_many association, can I limit the  
> number returned on that has\_many association. I know I can limit the  
> amount of records returned, but how do I limit how many are returned on  
> its association?  
> If there is a way of limiting the includes explicitely when using the  
> finder, I’m not aware of it.But you can use :limit as one of the options  
> when you are defining the has\_many association itself at the model, so  
> you could so something like:

has\_many :detail\_items, :limit=\>10

There are also a number of other options you can use to further  
selecting which of the detail\_items will be retrieved for this  
association, like :order or :select.

Regards,

javier ramirez

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [May 16, 2007, 2:01am UTC](https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256/3 "2007-05-16T02:01:07Z")

</div>

ActiveRecord can’t currently do that, but it’d be a nice feature given  
how difficult it is to customize the eager loading queries.

However, you might be able emulate the functionality by constructing  
your own query to fetch the ids of the association that match. Then  
you could put them in a big array and pass them into the conditions on  
the final query.

There would be a lot of issues, and it might ultimately be slower than  
the n+1 queries. There’s no direct way to limit the secondary results  
although you might be able to cook something up with subselects or  
other advanced db features.

On May 15, 4:03 pm, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [May 16, 2007, 2:20am UTC](https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256/4 "2007-05-16T02:20:22Z")

</div>

I was thinking about that Javier, but I think it would ignore the :limit  
when it gets eager loaded.

Have you heard of it working?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [May 16, 2007, 2:56am UTC](https://www.ruby-forum.com/t/limit-on-eager-loaded-association/104256/5 "2007-05-16T02:56:11Z")

</div>

Aryk G. wrote:

> I was thinking about that Javier, but I think it would ignore the :limit  
> when it gets eager loaded.

to be honest i assumed it would work when eager loading but didn’t try  
before. I’m successfully using it for a model in which i’m not using  
eager-loading (just for a combo with ajax, so no need for the details  
beforehand) and I thought it would be just fine when including. I just  
tested that on the console and you are right, it will totally ignore the  
limit option in that case.

I understand the sql for limiting on the association would get a bit  
tricky, but I was hoping AR would apply the limit when mapping the  
results. Unfortunately it’s not.

regards,

javier
