Hello, I have just started using rails and I am wondering how I can prevent a query being executed for each row in my list view. I have 2 models: OrganizationType and Organization which has a one-to- many relationship through association in the models. In the controller when Organization.all is being executed I see that select statement is created that joins both tables as expected. However in the list I output OrganizationType''s description with "<%=h organization.organization_type.description %>" and this seems to execute another query for the OrganizationType. I am assuming this is because of the organization_type method that has been added by rails in organization due to the association. How can I output the OrganizationType''s description from the @organizations variable without a query being executed for each row in the list? Thanks, Edwin. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-May-10 09:22 UTC
Re: How to prevent query being executed for each row in list
On 9 May 2010 17:26, Edwin <edwinhaver-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How can I output the OrganizationType''s description from the > @organizations variable without a query being executed for each row in > the list?You can use the ":include" option of AR to eager-load relationships: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002263> I have just started using railsWelcome! :-) I''d suggest not to worry too much (at this stage) about which queries are being executed when. While learning, I''d just focus on the higher-level operations of the framework. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.