Hey Jeremy :)
Thanks for the response :)
Your code is *almost* what I had already, but instead of adding the
bars an Array I want to add them to parent so in my views all I have
to deal with are the models (which is pretty important to me)

e.g.

Sequel::Model.raise_on_save_failure = false

params[:foo].each do |k,v|
  # Setup new Foo instance, don't save yet
  foo = Foo.new(v[:foo])

  # Setup new all Bar instances first, don't save yet
  v[:bar].each do |k,v|
    bar = Bar.new(v)
    bar.valid?

    # add them to the parent
    foo.bars << bar
  end

  # Do database interaction inside of a transaction
  ...
end

I dont seem to be able to do this because whenever I add something to
the dataset every method ends up calling the "add_associated_object"
which requires the parent to have a pk...

    association.rb (line 814)
    def add_associated_object(opts, o)
      raise(Sequel::Error, "model object #{model} does not have a
primary key") unless pk
      raise(Sequel::Error, "associated object #{o.model} does not have
a primary key") if opts.need_associated_primary_key? && !o.pk

Is this possible? From a brief look through the source I couldnt find
anything.

Cheers
Mike :)

On Apr 16, 12:31 am, Jeremy Evans <[email protected]> wrote:
> On Apr 15, 3:07 pm, mike  jones <[email protected]>
> wrote:
>
>
>
>
>
> > Hi
>
> > Just looking for a fews pointers on the best way of building and
> > validating trees of objects from a hash of values (ie when a form
> > posts). Its pretty straightforward to do when there is no validation,
> > however when there is, and it fails on a parent object you can no
> > longer add children to its association because it requires a primary
> > key.
> > This mean the tree from that point down doesn't get built so I can't
> > redirect to a form to show the errors.
>
> > For example..
>
> > params = {
> >   'foo' => {
> >     '0' => {
> >       'none_empty_field' => ""
> >       'bar' => {
> >         '0' => { 'thing' => '4' }
> >         '1' => { 'thing' => '4' }
> >       },
> >     ...
> >   }
>
> > }
>
> > class Foo < Sequel::Model
> >   one_to_many :bars
>
> >   def validate
> >     validates_presence :none_empty_field
> >   end
>
> > end
>
> > class Bar < Sequel::Model
> >   many_to_one :foo
> > end
>
> > params[:foo].each do |k,v|
> >   foo = Foo.new(v['none_empty_field]')
> >   foo.raise_on_save_failure = false
> >   foo.save # fails validation because of mission field
>
> >   v[:bar].each do |k,v|
> >     foo.bars << Bar.new(v) # this will fail because the foo instance
> > has no primary key
> >   end
> > end
>
> > Will fail when you try to add the associations.
>
> > Any help would be appreciated :)
>
>   params[:foo].each do |k,v|
>     # Setup new Foo instance, don't save yet
>     foo = Foo.new(v[:foo])
>     foo.raise_on_save_failure = false
>
>     bars = []
>     # Setup new all Bar instances first, don't save yet
>     v[:bar].each do |k,v|
>       bar = Bar.new(v)
>       bar.raise_on_save_failure = false
>       # Prepopulate the errors for each bar
>       bar.valid?
>       bars << bar
>     end
>
>     # Do database interaction inside of a transaction
>     success = Foo.db.transaction do
>       break unless foo.save
>       break unless bars.each do
>         break unless bar.save
>         foo.add_bar(bar)
>       end
>     end
>   end
>
> With the above approach, if success isn't true, that means one of the
> objects isn't valid.  The database won't be updated, and you can get
> the errors for foo or any of the bars by calling the errors method on
> them.
>
> I didn't actually try to run the above code, so it may not work, but
> hopefully it gives you a decent idea of how to handle the situation.
>
> 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