On Mar 16, 11:02 am, jv27243 <[email protected]> wrote:
> I'm working on an app that uses a two stage create.  A user clicks the
> new button and the app presents a form with default values in return.
> The user then fills in the form and hits the create button and the app
> validates the form data before saving to the DB.
>
> The problem I'm having is creating a temporary object with
> associations in the 'new' stage.  I don't want it to write-through to
> the DB until I call save.  The one-to-many add_/remove_ methods and
> the many-to-one = operator seem to both save the sub-object by
> default.  Is there a way to override this behavior?  Better yet, is
> there a way to build out a graph of the object that I can use in my
> rendering logic without committing any changes to the DB until the
> 'create' stage?

For the rendering logic, update the associations cache manually:

  Album = Class.new(Sequel::Model)
  Artist = Class.new(Sequel::Model)
  Tag = Class.new(Sequel::Model)
  Album.many_to_one :artist
  Album.one_to_many :tags

  album = Album.new
  album.associations[:artist] = Artist.new
  album.associations[:tags] = [Tag.new, Tag.new]
  album.artist
  album.tags

For the actual creation, use the nested_attributes plugin.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to