Hi, I have a rails app that uses these models: class CoreRouter < ActiveRecord::Base has_many :ports end class Port < ActiveRecord::Base belongs_to :core_router has_many :addresses end class Address < ActiveRecord::Base belongs_to :port end I have one main list page that shows all CoreRouters, the associated ports and the associated addresses. The problem is that I now have over 400 addresses and I''m seeing over 400 queries when I generate this page, taking about 10 seconds to load. I thought an answer may be Eager Loading: @core_routers = CoreRouter.find(:all, :include => [ :ports, :addresses ]) But that fails: ActiveRecord::ConfigurationError (Association named ''addresses'' was not found; perhaps you misspelled it?): I''m assuming this is because the CoreRouter class doesn''t have a direct association with the Address class. How can I get it to eagerly load the addresses that are associated with the ports? -- 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 -~----------~----~----~----~------~----~------~--~---
> Hi, > > I have a rails app that uses these models: > > class CoreRouter < ActiveRecord::Base > has_many :ports > end > > class Port < ActiveRecord::Base > belongs_to :core_router > has_many :addresses > end > > class Address < ActiveRecord::Base > belongs_to :port > end > > I have one main list page that shows all CoreRouters, the associated > ports and the associated addresses. The problem is that I now have over > 400 addresses and I''m seeing over 400 queries when I generate this page, > taking about 10 seconds to load. I thought an answer may be Eager > Loading: > > @core_routers = CoreRouter.find(:all, :include => [ :ports, :addresses > ]) > > But that fails: > > ActiveRecord::ConfigurationError (Association named ''addresses'' was not > found; perhaps you misspelled it?): > > I''m assuming this is because the CoreRouter class doesn''t have a direct > association with the Address class. > > How can I get it to eagerly load the addresses that are associated with > the ports?@core_routers = CoreRouter.find(:all, :include => {:ports => :addresses}) (it''s either that or...) :include => :ports => :addresses) I can never remember as well I''ve never actually done it :) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jun 12, 2007, at 12:41 PM, Philip Hallstrom wrote:>> Hi, >> >> I have a rails app that uses these models: >> >> class CoreRouter < ActiveRecord::Base >> has_many :ports >> end >> >> class Port < ActiveRecord::Base >> belongs_to :core_router >> has_many :addresses >> end >> >> class Address < ActiveRecord::Base >> belongs_to :port >> end >> >> I have one main list page that shows all CoreRouters, the associated >> ports and the associated addresses. The problem is that I now >> have over >> 400 addresses and I''m seeing over 400 queries when I generate this >> page, >> taking about 10 seconds to load. I thought an answer may be Eager >> Loading: >> >> @core_routers = CoreRouter.find(:all, :include => >> [ :ports, :addresses >> ]) >> >> But that fails: >> >> ActiveRecord::ConfigurationError (Association named ''addresses'' >> was not >> found; perhaps you misspelled it?): >> >> I''m assuming this is because the CoreRouter class doesn''t have a >> direct >> association with the Address class. >> >> How can I get it to eagerly load the addresses that are associated >> with >> the ports? > > @core_routers = CoreRouter.find(:all, > :include => {:ports => :addresses}) > > (it''s either that or...)Yeah, it''s that. and if you need to include a :conditions option when you have :include''s, you need to indicate the table such as :conditions => [ ''core_routers.installed_on < ? AND ports.in_use = ?'', 1.year.ago.beginning_of_day.to_s(:db), true ] -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom wrote:> > @core_routers = CoreRouter.find(:all, > :include => {:ports => :addresses}) >That worked great... Dropped my DB time down from about 2 seconds to less than .5 seconds... -- 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> > and if you need to include a :conditions option when you > have :include''s, you need to indicate the table such as > :conditions => [ ''core_routers.installed_on < ? AND ports.in_use = ?'', > 1.year.ago.beginning_of_day.to_s(:db), true ] >Thanks Rob - I don''t need this yet, but might! -- 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 -~----------~----~----~----~------~----~------~--~---