Clemens Wyss
2006-Jul-02 09:21 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
On 7/2/06, Clemens Wyss <clemens.wyss@gmx.ch> wrote:> > 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)?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> Clemens > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060702/3ccb2590/attachment.html
Reasonably Related Threads
- Generic SingleTable inheritance
- Rails 2.2.2 and globalize - undefined method `render_file' for class `ActionView::Base' (NameError)
- Cannot compile 3.0.x on Solaris 9
- [PATCH] Fix qemu-dm segfault when multiple HVM domains ---Was: qemu-dm segfault with multiple HVM domains?
- [LLVMdev] Disjoint types after reading several modules