Hey Tim, <br><br><a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html">http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html</a><br><br>Explains all of the association methods and how they will behave with regards to saving objects and foreign keys etc:<br>
<br>"<br><h3>One-to-one associations</h3>
<ul><li><p>
Assigning an object to a <tt><a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one">has_one</a></tt>
 association
automatically saves that object and
</p>
</li></ul>
<p>
the object being replaced (if there is one), in order to update their
primary keys - except if the parent object is unsaved (<tt>new_record? 
==
true</tt>).
</p>
<ul><li><p>
If either of these saves fail (due to one of the objects being invalid) 
the
assignment
</p>
</li></ul>
<p>
statement returns <tt>false</tt> and the assignment is cancelled.
</p>
<ul><li><p>
If you wish to assign an object to a <tt><a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one">has_one</a></tt>
 association
without saving it,
</p>
</li></ul>
<p>
use the <tt>association.build</tt> method (documented below).
</p>
<ul><li><p>
Assigning an object to a <tt><a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to">belongs_to</a></tt>
association does not save the object, since
</p>
</li></ul>
<p>
the foreign key field belongs on the parent. It does not save the parent
either."</p><br><div class="gmail_quote">On 23 September 2010 14:43, Anthony Green <span dir="ltr"><<a href="mailto:Anthony.Green@bbc.co.uk">Anthony.Green@bbc.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
I usually use build in similar circumstances<br>
<br>
Person.build_underpants(:type => 'boxers')<br>
<div><div></div><div class="h5"><br>
<br>
On 23/09/2010 14:34, "Tim Cowlishaw" <<a href="mailto:tim@timcowlishaw.co.uk">tim@timcowlishaw.co.uk</a>> wrote:<br>
<br>
> Hi all,<br>
><br>
> Could I pick your collective brains about a little activerecord issue I'm<br>
> having?<br>
><br>
> Let's say you have two AR models:<br>
><br>
> class Person < ActiveRecord::Base<br>
> has_one :underpants<br>
> end<br>
><br>
> Underpants < ActiveRecord::Base<br>
> belongs_to :person<br>
> end<br>
><br>
> person = Person.create<br>
> yfronts = Underpants.create<br>
> boxers = Underpants.create<br>
><br>
> person.underpants = boxers<br>
> person.save<br>
><br>
> Now, my app  allows people to test out underpants before they commit to them,<br>
> so I want to set the underpants association on person, and keep the updated<br>
> person in memory, without that change hitting the database until I explicitly<br>
> save.<br>
><br>
> But<br>
> person.underpants = yfronts<br>
><br>
> immediately updates the foreign key in the database. I can see why this<br>
> happens - as the foreign key for the association is on my underpants table, it<br>
> wouldn't be updated on hitting 'save' on the person, so it's done eagerly.<br>
> However, is there any way to avoid this happening, and deal with the<br>
> consequences myself?<br>
><br>
> tl;dr: can I set a has_one association on an activerecord object in memory<br>
> only, without touching the DB?<br>
><br>
> Cheers.<br>
><br>
> Tim<br>
<br>
</div></div>--<br>
Anthony Green<br>
<br>
<br>
<br>
<br>
<br>
<br>
<a href="http://www.bbc.co.uk/" target="_blank">http://www.bbc.co.uk/</a><br>
This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.<br>
If you have received it in error, please delete it from your system.<br>
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.<br>
Please note that the BBC monitors e-mails sent or received.<br>
Further communication will signify your consent to this.<br>
<div><div></div><div class="h5"><br>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</div></div></blockquote></div><br>