[LRUG] Updating activerecord associations without saving

Anthony Eden anthonyeden at gmail.com
Thu Sep 23 06:58:24 PDT 2010


Again, how about

class Person
  attr_accessor :underpants_i_am_trying
end

person.underpants_i_am_trying = boxers

When you're ready to commit to those underpants:

person.underpants = person.underpants_i_am_trying

-Anthony

On Thu, Sep 23, 2010 at 3:54 PM, Tim Cowlishaw <tim at timcowlishaw.co.uk>wrote:

>
>
> Actually sorry, I haven't explained this that well.
>
> Looking at my original example:
>
>
> We have
>
> person.underpants = boxers
> person.save
>
> then we call
>
> person.underpants.build(:style => 'yfronts')
>
> this doesn't save the yfronts instance, sure, but it DOES null out the
> foreign key constraint on the 'boxers' row - thus rendering our customer
> underpantless untill they commit to wearing the yfronts
>
> (btw, this isn't the real business domain of the app we're working on)
>
> So, the problem isn't that the new record gets saved (it doesn't), but that
> the old one is prematurely removed from the association.
>
> I realise it's a bit of a corner case, but it seems like an unlikely bit of
> behavour.
>
> Thanks for your help, all!
>
> Cheers
>
> Tim
>
> On 23 Sep 2010, at 14:49, Tim Cowlishaw wrote:
>
>
> On 23 Sep 2010, at 14:47, Mr Jaba wrote:
>
>
>    -
>
>    If you wish to assign an object to a has_one<http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one>association without saving it,
>
> use the association.build method (documented below).
>
>
> Ok, see - that's what I thought, but it's clearly doing something different
> for me! Occam's razor says I probably have a callback lying around that I
> hadn't thought about, that's being too eager about saving things. Will
> double check!
>
> Thanks for the help!
>
> Cheers,
>
> Tim
>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>


-- 
GMU/IT d- s: a33 C++(++++)$ UL@ P--- L+(++) !E W+++$ !N o? K? w--- !O M++ V
PS+ PE Y PGP t+ !5 X- R tv b++ DI+ D++ G- e++ h---- r+++ y++++**

http://anthony.mp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20100923/0939e639/attachment-0003.html>


More information about the Chat mailing list