# Belongs\_to with global cache

**URL:** <https://www.ruby-forum.com/t/belongs-to-with-global-cache/178228>\
**Category:** Rails\
**Created:** [October 31, 2009, 8:40am UTC](https://www.ruby-forum.com/t/belongs-to-with-global-cache/178228 "2009-10-31T08:40:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![notid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@notid](https://www.ruby-forum.com/u/notid)\
**Post date:** [October 31, 2009, 8:40am UTC](https://www.ruby-forum.com/t/belongs-to-with-global-cache/178228/1 "2009-10-31T08:40:49Z")

</div>

Hi,

I’m pretty new to active record, so I’m not sure if the way I’m trying  
to do this is wrong. I’ve trimmed down my example to as small as I can  
think of. Suppose I have something like this:

class Person \< ActiveRecord::Base  
attr\_accessor :runtime\_property  
belongs\_to :brother, :class\_name =\> ‘Person’ :foreign\_key =\>  
‘brother\_id’  
end

At application startup time, I’m going to query for all of the People.  
These notes can refer to one another, and there is some data that the  
models have that is only pertinent during the uptime of the application.  
It took me a while to realize this, but when I go from one brother to  
the other through the belongs\_to relationship, it’s going to fetch and  
cache that object. Then when I go from the related object back to the  
first (they reciprocate one another), it does another query and is now  
pointing to a different object. What I’d like is if I went through this  
relationship to the related object, and then back to the original  
object, I would get the original object’s instance.

This is a difficult thing to describe. Maybe a better way of saying it  
that I want to make sure that there is only one instance per row of an  
object so that the runtime properties are always available.

I appreciate any help with this.

Thanks,  
Bryce

---

<div class="post-metadata">

**Author:** ![notid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@notid](https://www.ruby-forum.com/u/notid)\
**Post date:** [October 31, 2009, 11:22am UTC](https://www.ruby-forum.com/t/belongs-to-with-global-cache/178228/2 "2009-10-31T11:22:41Z")

</div>

On Oct 31, 7:40 am, Bryce C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This is a difficult thing to describe. Maybe a better way of saying it  
> that I want to make sure that there is only one instance per row of an  
> object so that the runtime properties are always available.

That’s sometimes knows as an identity map and is something that  
activerecord just doesn’t have. You may find that one way out is for  
your class to have a map of ids to these runtime properties.

Fred

---

<div class="post-metadata">

**Author:** ![notid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@notid](https://www.ruby-forum.com/u/notid)\
**Post date:** [November 1, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/belongs-to-with-global-cache/178228/3 "2009-11-01T17:45:57Z")

</div>

Frederick C. wrote:

> On Oct 31, 7:40ï¿½am, Bryce C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > This is a difficult thing to describe. Maybe a better way of saying it  
> > that I want to make sure that there is only one instance per row of an  
> > object so that the runtime properties are always available.
> 
> That’s sometimes knows as an identity map and is something that  
> activerecord just doesn’t have. You may find that one way out is for  
> your class to have a map of ids to these runtime properties.
> 
> Fred

Thank you. I ended up doing something similar.
