Question... if i''ve got a one-to-one relationship between Assets and Tags (simple example), and I do: a = Asset.find 1, :include => :tag I can do "a.tag" without hitting the database again. But "a.tag.asset" instantiates a new Asset object, with a new database query... even though it''s a reference back to the original object. Now I''ve got two instances of the same object, which could be confusing. Is there a way to make ActiveRecord "smart" enough to know that an instance of a given record is already initialized? Thanks, Norman --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Without hacking the source code I would think not. ActiveRecord would usually do some caching in situations like this, but because your initial query is :include => "tag", that goes out the window. 9, 2008 at 10:18 AM, Norman Elton <normelton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Question... if i''ve got a one-to-one relationship between Assets and > Tags (simple example), and I do: > > a = Asset.find 1, :include => :tag > > I can do "a.tag" without hitting the database again. But "a.tag.asset" > instantiates a new Asset object, with a new database query... even > though it''s a reference back to the original object. Now I''ve got two > instances of the same object, which could be confusing. > > Is there a way to make ActiveRecord "smart" enough to know that an > instance of a given record is already initialized? > > Thanks, > > Norman > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 19 May 2008, at 02:21, Ryan Bigg (Radar) wrote:> Without hacking the source code I would think not. > > ActiveRecord would usually do some caching in situations like this, > but because your initial query is :include => "tag", that goes out > the window. >I believe http://svn.techno-weenie.net/projects/plugins/active_record_context/ solves this problem because it caches loaded records by id (rather than caching the results of an sql query, keyed by that query). Fundamentally it would be nice if rails knew which the ''reverse'' association was, but right now that''s not the case. Fred> > 9, 2008 at 10:18 AM, Norman Elton <normelton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Question... if i''ve got a one-to-one relationship between Assets and > Tags (simple example), and I do: > > a = Asset.find 1, :include => :tag > > I can do "a.tag" without hitting the database again. But "a.tag.asset" > instantiates a new Asset object, with a new database query... even > though it''s a reference back to the original object. Now I''ve got two > instances of the same object, which could be confusing. > > Is there a way to make ActiveRecord "smart" enough to know that an > instance of a given record is already initialized? > > Thanks, > > Norman > > > > > > -- > Ryan Bigg > http://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email. > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---