# SQL calls and associations

**URL:** <https://www.ruby-forum.com/t/sql-calls-and-associations/92920>\
**Category:** Rails\
**Created:** [April 25, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/sql-calls-and-associations/92920 "2007-04-25T18:11:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 25, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/sql-calls-and-associations/92920/1 "2007-04-25T18:11:40Z")

</div>

Hi all,

I have a really quick question on the number of SQL calls I am seeing in  
my development log when it comes to associations. This may just be an  
environment issue, but I could not find an answer.

If I have a belongs\_to association for books to a library for example.

class Library \< ActiveRecord::Base  
has\_many :books  
end

class Book \< ActiveRecord::Base

end

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 25, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/sql-calls-and-associations/92920/2 "2007-04-25T18:17:37Z")

</div>

Peer A. wrote:

> end
> 
> class Book \< ActiveRecord::Base
> 
> end

Could you elaborate on the problem more? What are you calling in your  
application that seems to be generating a lot of extra SQL calls?

@library = Library.find(10)

Something like this?

@library.books.each {}

If so, you might look at the :include option for .find() which will  
prevent a lot of extra queries.

-Robby

–  
Robby R.  
[http://www.robbyonrails.com/](http://www.robbyonrails.com/)

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 25, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/sql-calls-and-associations/92920/3 "2007-04-25T18:18:33Z")

</div>

Hi all,

Sorry about the previous message I accidentally sent it before  
completing it.

I have a really quick question on the number of SQL calls I am seeing in  
my development log when it comes to associations. This may just be an  
environment issue, but I could not find an answer.

If I have a belongs\_to association for books to a library for example.

class Library \< ActiveRecord::Base  
has\_many :books  
end

class Book \< ActiveRecord::Base  
belongs\_to :library  
end

If I am iterating over a collection of books in a view like so,

\<% for b in @books %\>  
\<%=h b.name %\>,  
\<%= b.library.name %\>  
\<%= b.library.city %\>, \<%= b.library.state %\>  
\<% end %\>

For each time through the loop there are 3 SQL call for the Library  
information. One for each of the outputs where the Library is  
referenced. Is this how this is supposed to work? Again, this is all  
in a development environment, does the production behave differently?  
Or, is there some kind of caching mechanism I am not taking advantage  
of?

Thanks,

Peer

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 25, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/sql-calls-and-associations/92920/4 "2007-04-25T18:50:41Z")

</div>

On 4/25/07, Peer A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> Sorry about the previous message I accidentally sent it before  
> completing it.
> 
> I have a really quick question on the number of SQL calls I am seeing in  
> my development log when it comes to associations. This may just be an  
> environment issue, but I could not find an answer.

[…]

[#22 Eager Loading - RailsCasts](http://railscasts.com/episodes/22) is what you’re looking for.
