You could store the associated record as a simple instance variable on the model.<div><br></div><div>Alternatively maybe the fact that someone is trying on the underwear before buying it *is* something you want to keep track of, just through another association?</div>
<div><br></div><div>Sincerely,</div><div>Anthony Eden<br><br><div class="gmail_quote">On Thu, Sep 23, 2010 at 3:34 PM, Tim Cowlishaw <span dir="ltr"><<a href="mailto:tim@timcowlishaw.co.uk">tim@timcowlishaw.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi all,<br>
<br>
Could I pick your collective brains about a little activerecord issue I'm 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, so I want to set the underpants association on person, and keep the updated person in memory, without that change hitting the database until I explicitly save.<br>

<br>
But<br>
person.underpants = yfronts<br>
<br>
immediately updates the foreign key in the database. I can see why this happens - as the foreign key for the association is on my underpants table, it wouldn't be updated on hitting 'save' on the person, so it's done eagerly. However, is there any way to avoid this happening, and deal with the consequences myself?<br>

<br>
tl;dr: can I set a has_one association on an activerecord object in memory only, without touching the DB?<br>
<br>
Cheers.<br>
<br>
Tim<br>
<br>
<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>
</blockquote></div><br><br clear="all"><br>-- <br>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++++**<br><br><a href="http://anthony.mp">http://anthony.mp</a><br>

</div>