foo = Foo.find(:first, :include => [:bars])
If the association has associations you can do:
foo = Foo.find(;first, :include => [{:bars => :baz}])
This will do joins for you automatically.
itamar wrote:> Hi,
> I''m learning rails and if I understand correct the active record
always
> uses a "lazy" way to fetch relations (so if a record has a userId
and I
> want to display the userName, another query is executed).
> is that right? as far as I know this promotes n+1 problem in accessing
> the DB, and other approaches, like using joins, are more efficient. is
> there a way to make ActiveRecord eagerly fetch the required data?
--
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
-~----------~----~----~----~------~----~------~--~---