# Use of :include option in find query

**URL:** <https://www.ruby-forum.com/t/use-of-include-option-in-find-query/193053>\
**Category:** Rails\
**Created:** [August 10, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/use-of-include-option-in-find-query/193053 "2010-08-10T14:44:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 10, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/use-of-include-option-in-find-query/193053/1 "2010-08-10T14:44:12Z")

</div>

Hi,  
I have a Person model and Company model like as follows:-

Person.rb

class Person \<ActiveRecord::Base  
has\_one :company  
end

Company.rb  
class Company \< ActiveRecord::Base  
end

I have a function  
def person\_details  
@person=Person.find(params[:id, :include=\> :company)  
end

person\_details.html.erb  
\<%=@person.name%\> : \<%=@person.company.name%\>

I read that if you use the :include option then it reduces your number  
of queries.  
Means, whenever it is firing a query to find person same time it eager  
loads the company association.

I am confused that what exactly it does? Is it load the company object  
in memory? and instead of going to database it takes it from memory.

Can anyone elaborate me what exactly happens when we use the :include  
option.

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 10, 2010, 3:38pm UTC](https://www.ruby-forum.com/t/use-of-include-option-in-find-query/193053/2 "2010-08-10T15:38:01Z")

</div>

On 10 August 2010 13:44, Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%=@person.name%\> : \<%=@person.company.name%\>
> 
> I read that if you use the :include option then it reduces your number  
> of queries.  
> Means, whenever it is firing a query to find person same time it eager  
> loads the company association.
> 
> I am confused that what exactly it does? Is it load the company object  
> in memory? and instead of going to database it takes it from memory.

Write some code that uses your find and then accesses person.company  
and look in the log to see the queries being used. Repeat without the  
:include and you will see the difference.

Colin

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 11, 2010, 7:21am UTC](https://www.ruby-forum.com/t/use-of-include-option-in-find-query/193053/3 "2010-08-11T07:21:06Z")

</div>

Hi I have written code like this

1. Without :include  
My code is :-  
def user\_details  
@user=User.find(params[:id])  
@company=@user.company  
end  
Log Output:-

Processing UsersController#user\_details (for 127.0.0.1 at 2010-08-11  
10:51:34) [GET]  
Parameters: {“id”=\>“2”}  
User Columns (1.0ms) SHOW FIELDS FROM `users`  
User Load (0.4ms) SELECT \* FROM `users` WHERE (`users`.`id` = 2)  
Company Load (0.4ms) SELECT \* FROM `companies` WHERE  
(`companies`.user\_id = 2) LIMIT 1  
Rendering template within layouts/users  
Rendering users/user\_details  
Completed in 65ms (View: 48, DB: 2) | 200 OK  
[[http://localhost/user\_xml?id=2](http://localhost/user_xml?id=2)]

1. With Include:-  
def user\_details  
@user=User.find(params[:id], :include=\>:company)  
@company=@user.company  
end  
Log Output:-  
Processing UsersController#user\_details (for 127.0.0.1 at 2010-08-11  
10:53:13) [GET]  
Parameters: {“id”=\>“2”}  
User Columns (1.2ms) SHOW FIELDS FROM `users`  
User Load (0.5ms) SELECT \* FROM `users` WHERE (`users`.`id` = 2)  
Company Load (0.3ms) SELECT \* FROM `companies` WHERE  
(`companies`.user\_id = 2) LIMIT 1  
Rendering template within layouts/users  
Rendering users/user\_details  
Completed in 13ms (View: 2, DB: 2) | 200 OK  
[[http://localhost/user\_xml?id=2](http://localhost/user_xml?id=2)]

If you look at the both o/p there is a query on company table in both  
the cases.

What is the difference?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 11, 2010, 8:04am UTC](https://www.ruby-forum.com/t/use-of-include-option-in-find-query/193053/4 "2010-08-11T08:04:30Z")

</div>

The queries will likely be the same… but you should check to see if  
it loaded the company details _before_ you called @user.company.

You might try the following from script/console while tailing the log  
file in another terminal.

ruby script/console  
=\> User.find(2)

… check the log/development.log

=\> User.find(2, :include =\> :company)

… check log/development.log

Good luck!

Cheers,  
Robby
