Hi, I am a Sequel noob so please bear with me.
This is my first post to this group. I must say that of all the ORMs
out there in the ruby world this has been the best in terms of
flexibility so far.
(I gave up on AR and DM was too painful to install on a windows box)
Now, back to the question:
I have a fairly simple model with a 1-to-many association with a
composite key
Eg. (These are not the actual classes from my project - they are just
to illustrate the problem)
class Order
one_to_many :items , class=>"OrderItems", :key =>order_id
end
class OrderItems
end
What is the best way to save the order along with the order items in
one go?
(Save = create or update)
I tried the following approaches but didnt feel either was clean. Am I
missing something obvious?
a) Populate order and order items and call save on ALL objects
o=Order.new {...}
oi = [ OrderItems.new({..}), OrderItems.new({..})]
o.items.concat(oi)
(Also notice that o.items makes a select to create the empty array -
hence fails if I have not set the primary key yet. )
o.save() does not cause oi's to be saved.
I iterated over the list and called save on each ( in a transaction
along with o.save)
Nice thing abt this approach is that the same code works even for an
update. repost all the data, populate the entire object and call my
save.
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)
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?
Thanks!
Ajay
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---