Message from discussion
ActiveRecord: updating an association of an already persisted instance in memory without making the update persist right away. Is this possible?
Received: by 10.58.200.167 with SMTP id jt7mr4708782vec.16.1350564971727;
Thu, 18 Oct 2012 05:56:11 -0700 (PDT)
X-BeenThere: rubyonrails-core@googlegroups.com
Received: by 10.220.147.138 with SMTP id l10ls420922vcv.7.gmail; Thu, 18 Oct
2012 05:56:06 -0700 (PDT)
Received: by 10.52.96.71 with SMTP id dq7mr3485533vdb.11.1350564966569;
Thu, 18 Oct 2012 05:56:06 -0700 (PDT)
Date: Thu, 18 Oct 2012 05:56:06 -0700 (PDT)
From: ChuckE <honeyryderch...@gmail.com>
To: rubyonrails-core@googlegroups.com
Message-Id: <0af0aefc-4d31-4de0-acbe-07f6ce3f4e29@googlegroups.com>
Subject: ActiveRecord: updating an association of an already persisted
instance in memory without making the update persist right away. Is this
possible?
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_1550_29255775.1350564966033"
------=_Part_1550_29255775.1350564966033
Content-Type: multipart/alternative;
boundary="----=_Part_1551_28730637.1350564966033"
------=_Part_1551_28730637.1350564966033
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Hi,
I have ClassA, which has many objects of ClassB. Let's say that the
dependency of that relationship is of the destroy kind:
class ClassA < ActiveRecord::Base
has_many :class_bs, :dependency => :destroy
end
class ClassB < ActiveRecord::Base
belongs_to :class_a
end
Now let's say I already have a persisted instance of ClassA, and this
instance has also associated persisted objects:
a = ClassA.first
a.persisted #=> true
a.class_bs.length #=> >1
a.class_bs,all?{|b|b.persisted?} #=> true
So now, I would like to replace my associated class_bs with a new
collection of only one element, which is not persisted. I would like to
keep this only in memory, until I save the parent (or its child, for all I
care):
new_bs = [ClassB.new]
new_bs.all?{|b| b.new_record? } #=> true
a.class_bs = new_bs
a.class_bs.all?{|b| b.new_record?} #=> true
a.save
a.class_bs.all?{|b| b.new_record?} #=> false
Problem is, the way it works, step 4 will return "false" instead of true.
That is, as soon as I assign something to an already persisted parent, the
new associations are stored right away (unless they are invalid). This is
something that I don't like, because I may want to associate a to another
object x, and I want to validate everything before I persist the changes,
but the way it works, as soon as I get to x, changes were already made. Is
there a way to do this in AR? and still keep the dependency => destroy?
Thanks in advance
------=_Part_1551_28730637.1350564966033
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Hi,<br><br>I have ClassA, which has many objects of ClassB. Let's say that =
the dependency of that relationship is of the destroy kind:<br><br>class Cl=
assA < ActiveRecord::Base<br> has_many :class_bs, :dep=
endency =3D> :destroy<br>end<br>class ClassB < ActiveRecord::Base<br>=
belongs_to :class_a<br>end<br><br>Now let's say I alread=
y have a persisted instance of ClassA, and this instance has also associate=
d persisted objects:<br><br> a =3D ClassA.first<br> =
a.persisted #=3D> true<br> a.class_bs.length #=3D> >1=
<br> a.class_bs,all?{|b|b.persisted?} #=3D> true<br><br>So n=
ow, I would like to replace my associated class_bs with a new collection of=
only one element, which is not persisted. I would like to keep this only i=
n memory, until I save the parent (or its child, for all I care):<br><br>&n=
bsp; new_bs =3D [ClassB.new]<br> new_bs.all?{|b| b.new_re=
cord? } #=3D> true<br> a.class_bs =3D new_bs<br> =
a.class_bs.all?{|b| b.new_record?} #=3D> true<br> a.save<br=
> a.class_bs.all?{|b| b.new_record?} #=3D> false<br><br>Prob=
lem is, the way it works, step 4 will return "false" instead of true. That =
is, as soon as I assign something to an already persisted parent, the new a=
ssociations are stored right away (unless they are invalid). This is someth=
ing that I don't like, because I may want to associate a to another object =
x, and I want to validate everything before I persist the changes, but the =
way it works, as soon as I get to x, changes were already made. Is there a =
way to do this in AR? and still keep the dependency =3D> destroy? <br><b=
r>Thanks in advance<br>
------=_Part_1551_28730637.1350564966033--
------=_Part_1550_29255775.1350564966033--