Tuesday, August 10, 2010

[Rails] Use of :include option in find query

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
--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate