However, How would I go about re-adjusting the Product.quantity when
an item.quantity is CHANGED?
It seems like it should be a trivial thing, but I’m not able to wrap
my brain around it. Thanks for any help.
You might want to use active record’s change tracking. if you had an
item, item.quantity_changed? tells you whether the quantity has
changed, item.quantity_change returns an array where the first item is
the old value, the second is the new value, item.changes is a hash of
attribute names to pairs like that returned by quantity_change.
Then if quantity has increased by 2, decrease Product.quantity by 2.
You could do this in a before_save or after_save - this would ensure
that is the save fails at some other point (e.g. the order fails some
separate validation) all of the changes get rolled back. You should
also be vary wary of race conditions - i’d look into using optimistic
locking on the products table.
Fred
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.