Belongs_to with global cache

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

On Oct 31, 7:40 am, Bryce C. [email protected]
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

Frederick C. wrote:

On Oct 31, 7:40�am, Bryce C. [email protected]
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.