SQL calls and associations

Peer Allan wrote:

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

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