I. E. Smith-Heisters
2008-Dec-11 03:40 UTC
make members of a has_many share a reference to their belongs_to
Hi all, I have a model (Foo) with a has_many association to another model (Bar). Each Bar backreferences Foo, and calls a method on Foo that is expensive to call the first time per-instance (Foo#pricey). If all the instances of Bar referenced the same instance of Foo, the cost of calling Foo#pricey would only have to be paid once. Alas, this is not so:>> foo = Foo.find :first >> foo.object_id=> 70169390687560>> foo.bars[0].foo.object_id=> 70169406714940>> foo.bars[1].foo.object_id=> 70169407605180 I haven''t looked into the problem too much, since I thought this was perhaps a simple, common problem for which solution patterns already exist. Are there? Alternatively, can you recommend a good starting point for developing a pattern? Thanks, Ian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-11 08:59 UTC
Re: make members of a has_many share a reference to their belongs_to
On 11 Dec 2008, at 03:40, I. E. Smith-Heisters wrote:> > Hi all, > > I have a model (Foo) with a has_many association to another model > (Bar). Each Bar backreferences Foo, and calls a method on Foo that is > expensive to call the first time per-instance (Foo#pricey). If all the > instances of Bar referenced the same instance of Foo, the cost of > calling Foo#pricey would only have to be paid once. Alas, this is not > so: > >>> foo = Foo.find :first >>> foo.object_id > => 70169390687560 >>> foo.bars[0].foo.object_id > => 70169406714940 >>> foo.bars[1].foo.object_id > => 70169407605180 > > I haven''t looked into the problem too much, since I thought this was > perhaps a simple, common problem for which solution patterns already > exist. Are there? Alternatively, can you recommend a good starting > point for developing a pattern? >There''s certainly not an established solution to this, but there are various experiments such as http://github.com/h-lame/parental_control/tree/master 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-/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 -~----------~----~----~----~------~----~------~--~---
Ian Smith-Heisters
2008-Dec-13 06:34 UTC
Re: make members of a has_many share a reference to their belongs_to
On Thu, Dec 11, 2008 at 12:59 AM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 11 Dec 2008, at 03:40, I. E. Smith-Heisters wrote: > >> >> Hi all, >> >> I have a model (Foo) with a has_many association to another model >> (Bar). Each Bar backreferences Foo, and calls a method on Foo that is >> expensive to call the first time per-instance (Foo#pricey). If all the >> instances of Bar referenced the same instance of Foo, the cost of >> calling Foo#pricey would only have to be paid once. Alas, this is not >> so: >> >>>> foo = Foo.find :first >>>> foo.object_id >> => 70169390687560 >>>> foo.bars[0].foo.object_id >> => 70169406714940 >>>> foo.bars[1].foo.object_id >> => 70169407605180 >> >> I haven''t looked into the problem too much, since I thought this was >> perhaps a simple, common problem for which solution patterns already >> exist. Are there? Alternatively, can you recommend a good starting >> point for developing a pattern? >> > > There''s certainly not an established solution to this, but there are > various experiments such as http://github.com/h-lame/parental_control/tree/master > > Fred >Cool, thanks Fred. Unfortunately the one relationship he doesn''t support is belongs_to <-> has_many :( No matter, i solved the problem the old fashioned way by just optimizing the hell out of it. Which is, of course, preferable in many ways. Still, that''s some neat code and could well come in handy in other ways. Thanks again, ISH> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---