On Nov 17, 2:58 am, Ajay CB <[EMAIL PROTECTED]> wrote:
> a) Populate order and order items and call save on ALL objects
>
> o=Order.new {...}
> oi = [ OrderItems.new({..}), OrderItems.new({..})]
> o.items.concat(oi)

This doesn't work, as Sequel does not use proxies for associations.
o.items is just a plain array that holds the cache of associated
objects, modifying it has no effect on the database.

> b) Using the add_ methods
>
> o=Order.new {...}
> [ OrderItems.new({..}), OrderItems.new({..})].each {|x| o.add_items
> x }
> at this point the inserts for order items are already done - this is
> not what I want as I am not yet ready to save it.
>
> This works fine for create.
>
> Problem with this is when dealing with updates.  say we allow ppl to
> Add/Edit order items at a later point in the UI.
> Then I cant just to a o.add_ ([...]) coz it throws a dup key exception
> (I am using mysql)

This is the correct way to handle it.  To handle updates:

  o.add_item(x) unless o.items.include?(x)

> Any suggestions on whats the best way to deal with CUD (of CRUD) along
> with associations?
>
> Is there a simpler API where I can call a "save" and it recursively
> updates all the associated models?
> If this is a useful feature I could contribute (that way I can get my
> hands dirty too) - Any thoughts?

No.  Associations are not proxies, so this won't work as you think.
Just use the add_ and remove_ methods.  If you are unsure if the item
you are adding already exists in the association, you need to check it
manually.  You can do this by overriding add_item and calling super
only if the item isn't already in the association, and you may want to
go that route if it makes your code simpler.

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