# help with :include on active record finds

**URL:** <https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338>\
**Category:** rubyonrails-talk\
**Created:** [June 6, 2008, 9:15pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338 "2008-06-06T21:15:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_O\_Hara](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sean\_O\_Hara](https://discuss.rubyonrails.org/u/Sean_O_Hara)\
**Post date:** [June 6, 2008, 9:15pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/1 "2008-06-06T21:15:42Z")

</div>

Hi All,

I am having trouble getting the :include option to work on my active record finds. They are not eager loading, adding the joins to the sql queries. Has anyone had this problem? Here is my query:

@cart = Cart.find\_by\_id(session[:cart\_id], :include =\> [:cart\_items])

This is generating the following query: SELECT \* FROM `carts` WHERE (`carts`.`id` = 23) LIMIT 1

Rails is not eager loading the cart\_items which belongs\_to cart. Does anyone know why?

Thanks,

Sean

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [June 6, 2008, 9:20pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/2 "2008-06-06T21:20:26Z")

</div>

> Hi All, I am having trouble getting the :include option to work on my active record finds. They are not eager loading, adding the joins to the sql queries. Has anyone had this problem? Here is my query:
> 
> &nbsp;&nbsp;&nbsp;@cart = Cart.find\_by\_id(session[:cart\_id], :include =\> [:cart\_items])
> 
> This is generating the following query: SELECT \* FROM `carts` WHERE (`carts`.`id` = 23) LIMIT 1
> 
> Rails is not eager loading the cart\_items which belongs\_to cart. Does anyone know why?

Don't think it can. Not in a single query. You have one cart that has many items. You can't return all of that in a single query unless you want to send duplicate rows for the cart for every row of the cart item.

There may be right after that line in the logs a line fetching the cart items. Or Rails might not do it since it's not possible.

If I'm thinking clearly that is 🙂

---

<div class="post-metadata">

**Author:** ![Pardee\_Roy](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Pardee\_Roy](https://discuss.rubyonrails.org/u/Pardee_Roy)\
**Post date:** [June 6, 2008, 10:16pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/3 "2008-06-06T22:16:52Z")

</div>

Nah, that should work I think--see the "Eager Loading" section on [http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod) s.html

I just tried this out in a toy app of my own, and was surprised to see in my log that \*in addition\* to the left-join query immediately under the heading "Processing OrganizationsController#show", there were 3 other queries that followed the heading "Rendering organizations/show". One was triggered by (I think) this line in my view:

&nbsp;&nbsp;\<% if @organization.people.count \> 0 %\>

That generated a:

&nbsp;&nbsp;SELECT count(\*) AS count\_all FROM `people` WHERE (people.organization\_id = 1)

Then I have 2 instances of:

&nbsp;&nbsp;SELECT \* FROM `organizations` WHERE (`organizations`.`id` = 1)

Which I believe are caused by resolving the person.organization.name calls in the part of the view that lists the people belonging to @organization.

So... not that this isn't a bit dismaying, BUT--is there any chance that you too are looking at calls generated not by your find\_by\_id call, but by subsequent view activity?

Cheers,

-Roy

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Hara](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sean\_O\_Hara](https://discuss.rubyonrails.org/u/Sean_O_Hara)\
**Post date:** [June 6, 2008, 10:27pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/4 "2008-06-06T22:27:21Z")

</div>

I don’t think it has to do with view activity, because the cart and cart\_items queries are generated by a before\_filter… and the are only run once. Plus, they are instantiating the @cart and it’s .cart\_items children, so I don’t think any view methods would affect that. Maybe in your case you can call @organization.people.size to avoid the sql. There are other finds in my site that aren’t generating the eager loading queries like I would expect (and I believe rails used to, prior to rails 2.0.2).

Here’s the kind of query I would expect rails to generated based on the find statement:

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 6, 2008, 10:33pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/5 "2008-06-06T22:33:39Z")

</div>

Eager loading in rails 2.1 changed: [Mixing :include and :conditions - Space Vatican](http://www.spacevatican.org/2008/4/29/include-and-conditions)

Fred

---

<div class="post-metadata">

**Author:** ![Pardee\_Roy](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Pardee\_Roy](https://discuss.rubyonrails.org/u/Pardee_Roy)\
**Post date:** [June 6, 2008, 10:43pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/6 "2008-06-06T22:43:34Z")

</div>

Ah--you're right about changing .count to .size. That does dissappear the count(\*) query. Thanks!

Like I say--I do get the LEFT JOIN query. It's not as pretty as yours, but it's there:

&nbsp;&nbsp;SELECT `organizations`.`id` AS t0\_r0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`name` AS t0\_r1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`abbreviation` AS t0\_r2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`city` AS t0\_r3, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`state` AS t0\_r4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`crn_site` AS t0\_r5, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`created_at` AS t0\_r6, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`organizations`.`updated_at` AS t0\_r7, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`id` AS t1\_r0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`first_name` AS t1\_r1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`last_name` AS t1\_r2, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`birth_date` AS t1\_r3, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`organization_id` AS t1\_r4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`created_at` AS t1\_r5, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people`.`updated_at` AS t1\_r6 &nbsp;&nbsp;FROM `organizations` LEFT OUTER JOIN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`people` &nbsp;&nbsp;ON people.organization\_id = organizations.id WHERE (`organizations`.`id` = '1')

That's with rails 2.0.2 and mysql. What's your version & db?

(There's no chance that you left off the 'has\_many :cart\_items' declaration in your cart class is there?)

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 7, 2008, 6:14pm UTC](https://discuss.rubyonrails.org/t/help-with-include-on-active-record-finds/30338/7 "2008-06-07T18:14:33Z")

</div>

> > Hi All, I am having trouble getting the :include option to work on my  
> > active record finds. They are not eager loading, adding the joins to the sql  
> > queries. Has anyone had this problem? Here is my query:
> > 
> > @cart = Cart.find\_by\_id(session[:cart\_id], :include =\>  
> > [:cart\_items])
> > 
> > This is generating the following query: SELECT \* FROM `carts` WHERE (`carts`.`id` = 23) LIMIT 1
> > 
> > Rails is not eager loading the cart\_items which belongs\_to cart.  
> > Does anyone know why?
> 
> Don't think it can. Not in a single query. You have one cart that  
> has many items. You can't return all of that in a single query unless you want to send duplicate rows for the cart for every row of the cart  
> item.

That's what Rails used to do 🙂

Fred
