Max Williams
2010-Jun-08 09:52 UTC
Can i eager-load associations onto an already-loaded object?
Let''s say that i have a User object in @user, already loaded. I want to constantly refer to @user.role on a particular page (where User belongs_to :role), so i want to ''eager load'' the role association. For the sake of argument, let''s say i wasn''t able to do this in the normal way (ie with the :include option to a find call) when i loaded the user out of the database in the first place, but need to do it subsequent to that. Is that possible? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Williams
2010-Jun-08 10:25 UTC
Re: Can i eager-load associations onto an already-loaded object?
Max Williams wrote:> Let''s say that i have a User object in @user, already loaded. I want > to constantly refer to @user.role on a particular page (where User > belongs_to :role), so i want to ''eager load'' the role association. > > For the sake of argument, let''s say i wasn''t able to do this in the > normal way (ie with the :include option to a find call) when i loaded > the user out of the database in the first place, but need to do it > subsequent to that. Is that possible?Just to answer my own question, sort of, i think that i can do @user[:role] = @user.role and that sets the instance variable which is used for subsequent calls to @user.role, effectively eager loading that association. is that correct? Is this the same thing as the eager loading would do? eg, is it precisely as prone to weird side effects as the regular eager loading? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jun-08 10:41 UTC
Re: Can i eager-load associations onto an already-loaded object?
On Jun 8, 11:25 am, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Max Williams wrote: > > Let''s say that i have a User object in @user, already loaded. I want > > to constantly refer to @user.role on a particular page (where User > > belongs_to :role), so i want to ''eager load'' the role association. > > > For the sake of argument, let''s say i wasn''t able to do this in the > > normal way (ie with the :include option to a find call) when i loaded > > the user out of the database in the first place, but need to do it > > subsequent to that. Is that possible? > > Just to answer my own question, sort of, i think that i can do > > @user[:role] = @user.role > > and that sets the instance variable which is used for subsequent calls > to @user.role, effectively eager loading that association. is that > correct? Is this the same thing as the eager loading would do? eg, is > it precisely as prone to weird side effects as the regular eager > loading?It''s not at all what normal eager loading would do. if you do class User < ActiveRecord::Base class << self public :preload_associations end end then you can do User.preload_associations [user1, user2, user3], :role (at least in rails 2.x - might very easily not work in 3.x) Fred Fred> -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jun-08 10:43 UTC
Re: Can i eager-load associations onto an already-loaded object?
On Jun 8, 11:41 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 8, 11:25 am, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > then you can do > > User.preload_associations [user1, user2, user3], :role >One addition - it''s pointless doing this unless you have more than 1 user object - it won''t be any faster Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Williams
2010-Jun-08 11:09 UTC
Re: Can i eager-load associations onto an already-loaded object?
Frederick Cheung wrote:> One addition - it''s pointless doing this unless you have more than 1 > user object - it won''t be any faster > > FredThanks fred - that works. Faster than what, though? Than my approach? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jun-08 12:50 UTC
Re: Can i eager-load associations onto an already-loaded object?
On Jun 8, 12:09 pm, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote: > > One addition - it''s pointless doing this unless you have more than 1 > > user object - it won''t be any faster > > > Fred > > Thanks fred - that works. Faster than what, though? Than my approach?compared to not eagerloading it at all. Fred> -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Williams
2010-Jun-08 13:21 UTC
Re: Can i eager-load associations onto an already-loaded object?
Frederick Cheung wrote:> On Jun 8, 12:09�pm, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Frederick Cheung wrote: >> > One addition - it''s pointless doing this unless you have more than 1 >> > user object - it won''t be any faster >> >> > Fred >> >> Thanks fred - that works. �Faster than what, though? �Than my approach? > > compared to not eagerloading it at all. > > FredIn this case, i have a page which loads a partial twenty times. Each partial calls a method on the user object (current_user as it happens) which inspects its associated role object. So, if i don''t eager load, then the role gets loaded twenty times. I might be misunderstanding your point though. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Jun-08 13:28 UTC
Re: Re: Can i eager-load associations onto an already-loaded object?
On 8 June 2010 14:21, Max Williams <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > In this case, i have a page which loads a partial twenty times. Each > partial calls a method on the user object (current_user as it happens) > which inspects its associated role object. So, if i don''t eager load, > then the role gets loaded twenty times. I might be misunderstanding > your point though.Memoize the role if necessary (although I thought AR would do that for you after the first load - not got time to check it out to be sure at the moment) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jun-08 13:31 UTC
Re: Can i eager-load associations onto an already-loaded object?
On Jun 8, 2:21 pm, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > Fred > > In this case, i have a page which loads a partial twenty times. Each > partial calls a method on the user object (current_user as it happens) > which inspects its associated role object. So, if i don''t eager load, > then the role gets loaded twenty times. I might be misunderstanding > your point though.If it''s the same user object throughout active record will cache the loaded role object for you - no need for eager loading here. Fred> -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Williams
2010-Jun-08 13:35 UTC
Re: Can i eager-load associations onto an already-loaded object?
Frederick Cheung wrote:> On Jun 8, 2:21�pm, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> > Fred >> >> In this case, i have a page which loads a partial twenty times. �Each >> partial calls a method on the user object (current_user as it happens) >> which inspects its associated role object. So, if i don''t eager load, >> then the role gets loaded twenty times. �I might be misunderstanding >> your point though. > > If it''s the same user object throughout active record will cache the > loaded role object for you - no need for eager loading here. > > Fredah i see, i knew mysql cached the repeated queries but didn''t know AR cached the retrieved association as well. thanks! max -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.