Thanks for the reply! > This is the correct way to handle it. To handle updates: > > o.add_item(x) unless o.items.include?(x)
Won't this skip the update if there is one? Ideally I need to find, modify and call save on the item. > 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. Can I actually override the add_item method? coz I thought it belonged to the Order (Model) class. I can try aliasing to something else and calling it though! The only pain is that I need to do this in more than one place. Few more question: Any reason only add_ and remove_ are there but not update_ ? Is it possible to have add_or_upd_ which will be more useful ? Atleast in mysql its possible to do this in one query using the 'insert...on duplicate key update' syntax (http://dev.mysql.com/doc/ refman/5.0/en/insert-on-duplicate.html) Its not standard SQL - but can be easily mimicked by catching dup key and firing an update for DBs that dont have an equivalent.(It is not atomic but atleast will work in 99.99% of the cases) Thanks! -Ajay On Nov 17, 9:17 pm, Jeremy Evans <[EMAIL PROTECTED]> wrote: > 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 -~----------~----~----~----~------~----~------~--~---
