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?
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?