[LRUG] Updating activerecord associations without saving

Mr Jaba the.jaba at gmail.com
Thu Sep 23 06:47:12 PDT 2010


Hey Tim,

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Explains all of the association methods and how they will behave with
regards to saving objects and foreign keys etc:

"
One-to-one associations

   -

   Assigning an object to a
has_one<http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one>association
automatically saves that object and

 the object being replaced (if there is one), in order to update their
primary keys - except if the parent object is unsaved (new_record? == true).


   -

   If either of these saves fail (due to one of the objects being invalid)
   the assignment

 statement returns false and the assignment is cancelled.

   -

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

   -

   Assigning an object to a
belongs_to<http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to>association
does not save the object, since

 the foreign key field belongs on the parent. It does not save the parent
either."

On 23 September 2010 14:43, Anthony Green <Anthony.Green at bbc.co.uk> wrote:

>
> I usually use build in similar circumstances
>
> Person.build_underpants(:type => 'boxers')
>
>
> On 23/09/2010 14:34, "Tim Cowlishaw" <tim at timcowlishaw.co.uk> wrote:
>
> > Hi all,
> >
> > Could I pick your collective brains about a little activerecord issue I'm
> > having?
> >
> > Let's say you have two AR models:
> >
> > class Person < ActiveRecord::Base
> > has_one :underpants
> > end
> >
> > Underpants < ActiveRecord::Base
> > belongs_to :person
> > end
> >
> > person = Person.create
> > yfronts = Underpants.create
> > boxers = Underpants.create
> >
> > person.underpants = boxers
> > person.save
> >
> > 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.
> >
> > But
> > person.underpants = yfronts
> >
> > 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?
> >
> > tl;dr: can I set a has_one association on an activerecord object in
> memory
> > only, without touching the DB?
> >
> > Cheers.
> >
> > Tim
>
> --
> Anthony Green
>
>
>
>
>
>
> http://www.bbc.co.uk/
> This e-mail (and any attachments) is confidential and may contain personal
> views which are not the views of the BBC unless specifically stated.
> If you have received it in error, please delete it from your system.
> Do not use, copy or disclose the information in any way nor act in reliance
> on it and notify the sender immediately.
> Please note that the BBC monitors e-mails sent or received.
> Further communication will signify your consent to this.
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20100923/217047d4/attachment.html>


More information about the Chat mailing list