# Make members of a has\_many share a reference to their belongs\_to

**URL:** <https://www.ruby-forum.com/t/make-members-of-a-has-many-share-a-reference-to-their-belongs-to/155369>\
**Category:** Rails\
**Created:** [December 11, 2008, 4:41am UTC](https://www.ruby-forum.com/t/make-members-of-a-has-many-share-a-reference-to-their-belongs-to/155369 "2008-12-11T04:41:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [December 11, 2008, 4:41am UTC](https://www.ruby-forum.com/t/make-members-of-a-has-many-share-a-reference-to-their-belongs-to/155369/1 "2008-12-11T04:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [December 11, 2008, 10:00am UTC](https://www.ruby-forum.com/t/make-members-of-a-has-many-share-a-reference-to-their-belongs-to/155369/2 "2008-12-11T10:00:12Z")

</div>

On 11 Dec 2008, at 03:40, I. E. Smith-Heisters wrote:

> > > foo = Foo.find :first  
> > > point for developing a pattern?

There’s certainly not an established solution to this, but there are  
various experiments such as

> **[GitHub - h-lame/parental\_control: A plugin for rails that allows has\_one,...](https://github.com/h-lame/parental_control/tree/master)**
>
> master

Fred

---

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [December 13, 2008, 7:35am UTC](https://www.ruby-forum.com/t/make-members-of-a-has-many-share-a-reference-to-their-belongs-to/155369/3 "2008-12-13T07:35:15Z")

</div>

On Thu, Dec 11, 2008 at 12:59 AM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > instances of Bar referenced the same instance of Foo, the cost of

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
