sreid
2011-Jul-17 22:20 UTC
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 ? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Jul-18 03:33 UTC
Re: refreshing multiple activerecord objects referring to same database record
On Mon, Jul 18, 2011 at 6:20 AM, sreid <sandy-QqoMRuB2QvA51pO8USHC0gC/G2K4zDHf@public.gmane.org> 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.> 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 ? > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
sreid
2011-Jul-18 07:48 UTC
Re: refreshing multiple activerecord objects referring to same database record
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 ? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Jul-18 08:01 UTC
Re: Re: refreshing multiple activerecord objects referring to same database record
On Mon, Jul 18, 2011 at 3:48 PM, sreid <sandy-QqoMRuB2QvA51pO8USHC0gC/G2K4zDHf@public.gmane.org> 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 Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Jul-18 08:29 UTC
Re: Re: refreshing multiple activerecord objects referring to same database record
On 18 July 2011 09:01, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Mon, Jul 18, 2011 at 3:48 PM, sreid <sandy-QqoMRuB2QvA51pO8USHC0gC/G2K4zDHf@public.gmane.org> 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 ?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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Jul-18 08:54 UTC
Re: Re: refreshing multiple activerecord objects referring to same database record
On 18 July 2011 09:29, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 July 2011 09:01, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> On Mon, Jul 18, 2011 at 3:48 PM, sreid <sandy-QqoMRuB2QvA51pO8USHC0gC/G2K4zDHf@public.gmane.org> 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 ? > > 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 http://edgerails.info/articles/what-s-new-in-edge-rails/2011/04/21/activerecord-identity-map/index.html though I don''t know whether this is entirely reliable. Colin> > You said you are unintentionally getting multiple in memory instances > of the same db record. How is that happening? > > Colin >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
sreid
2011-Jul-18 14:47 UTC
Re: refreshing multiple activerecord objects referring to same database record
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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Jul-18 15:43 UTC
Re: Re: refreshing multiple activerecord objects referring to same database record
On Mon, Jul 18, 2011 at 10:47 PM, sreid <sandy-QqoMRuB2QvA51pO8USHC0gC/G2K4zDHf@public.gmane.org> wrote:> 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 > >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.> 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. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
sreid
2011-Jul-18 15:52 UTC
Re: refreshing multiple activerecord objects referring to same database record
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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.