ActiveRecord's has_many, allways fetched from db?

Hi all,
say I have a
class Container
has_many :items
end
and
class Item
belongs_to :container
end

Now if I fetch a container from the db
cont = Container.find :first
and then call
cont.items
1000 times, does this result in 1000 db queries(for the items of the
container)?

Thanks
Clemens

On 7/2/06, Clemens W. [email protected] wrote:

Now if I fetch a container from the db
cont = Container.find :first
and then call
cont.items
1000 times, does this result in 1000 db queries(for the items of the
container)?

I believe it should hit the db for the find and then on the first
iteration
for items and then the results will be cached in the objects. If you
want
to eliminate the extra hit to the db then use the include option

Container.find( :first, :include => :items )

If your unsure of what’s happening, keep an eye on your development.log
file

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs