[LRUG] Updating activerecord associations without saving

Tim Cowlishaw tim at timcowlishaw.co.uk
Thu Sep 23 06:54:28 PDT 2010



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 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
> 

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


More information about the Chat mailing list