# A newbe question about active record

**URL:** <https://www.ruby-forum.com/t/a-newbe-question-about-active-record/103141>\
**Category:** Rails\
**Created:** [May 2, 2007, 9:09am UTC](https://www.ruby-forum.com/t/a-newbe-question-about-active-record/103141 "2007-05-02T09:09:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![itamar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@itamar](https://www.ruby-forum.com/u/itamar)\
**Post date:** [May 2, 2007, 9:09am UTC](https://www.ruby-forum.com/t/a-newbe-question-about-active-record/103141/1 "2007-05-02T09:09:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![itamar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@itamar](https://www.ruby-forum.com/u/itamar)\
**Post date:** [May 2, 2007, 9:23am UTC](https://www.ruby-forum.com/t/a-newbe-question-about-active-record/103141/2 "2007-05-02T09:23:17Z")

</div>

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?
