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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Robby Russell http://www.robbyonrails.com/ http://www.planetargon.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4/25/07, Peer Allan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.[...] http://railscasts.com/episodes/22 is what you''re looking for. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---