Refreshing multiple activerecord objects referring to same database record

I have an issue where I seem (unintentionally) to have more than 1
activerecord object/instance referring to the same database record.
How do I ensure that when I update the record, all instances are
refreshed with the same data ?

It sounds like “identity map” which is being added to activerecord for
rail 3.1 may solve this, but is there a solution for rails 3.0 ?

On Mon, Jul 18, 2011 at 6:20 AM, sreid [email protected] wrote:

I have an issue where I seem (unintentionally) to have more than 1
activerecord object/instance referring to the same database record.
How do I ensure that when I update the record, all instances are
refreshed with the same data ?

I don’t think this can be done on a global level. You need to call
reload
on
each of the instances to be sure that you have a fresh copy out of the
db.

http://groups.google.com/group/rubyonrails-talk?hl=en.

Is there some way to get a list of the existing instances which refer
to the record ?

Would upgrading to 3.1 and enabling identity map solve the problem ?

On Mon, Jul 18, 2011 at 3:48 PM, sreid [email protected] wrote:

Is there some way to get a list of the existing instances which refer
to the record ?

I think there’s none.

Would upgrading to 3.1 and enabling identity map solve the problem ?

Sorry, I haven’t looked at 3.1 yet so I have no clue.


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On 18 July 2011 09:01, Jim Ruther N. [email protected] wrote:

Would upgrading to 3.1 and enabling identity map solve the problem ?

I don’t think entity map addresses your problem, though I may be wrong.

You said you are unintentionally getting multiple in memory instances
of the same db record. How is that happening?

Colin

My problem occurs when trying to modify a parent record from a child,
using an after_create callback in the child is created (they have a
has_many / belongs_to association).

After some more testing (checking object ids), I can get a test to
pass if I use (e.g.) :

  1. Child.create(:parent => parent)
    but not
  2. parent.childs.create

I suspect that in 1), the child gets the original parent object passed
in, but in the second, it only gets the id, and, if needed, rails
creates a temp parent object from the id, which means I’m updating the
temp parent object (and the database if it is saved!), not the
original parent object in the parent.childs.create call, as I’d
expected. For the second way to work, I need always add a
parent.reload after a parent.childs.create.

Is this how rails/activerecord always works ? Is it a bug ?

Also, I did discover a way to find all objects related to a given
record
ObjectSpace.each_object(parent) do |temp_parent| if temp_parent.id
== parent.id …
, but it’s too slow.

For now, I just have to remember to always use the
Child.create(:parent => parent) form, but unless, someone has another
suggestion, I’ll likely migrate to rails 3.1 soon, to get identity
map. For me, this should give more intuitive behaviour for
activerecord.

On Mon, Jul 18, 2011 at 10:47 PM, sreid [email protected] wrote:

  1. should create a record with the same attributes as the record created
    in
    2).
    If this isn’t happening then you need to check your associations. Can
    you
    share some code
    regarding the associations?
    I’m sure you’re just giving an example above but parent.childs should be
    parent.children.

Also, I did discover a way to find all objects related to a given


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On 18 July 2011 09:29, Colin L. [email protected] wrote:

Would upgrading to 3.1 and enabling identity map solve the problem ?

I don’t think entity map addresses your problem, though I may be wrong.

On second thoughts I think it may solve the problem. I gather thought
that it is turned off by default as it is not entirely reliable yet.
See

though I don’t know whether this is entirely reliable.

Colin

I just used childs to show it was the plural. Would rails pluralize
child to children ?

All the attributes match correct in both cases - including parent_id -
but not parent. I assume the parent object is not a real attribute, as
only parent_id is shown when I do an inspect.