Skip to content

Instantly share code, notes, and snippets.

@sweatpantsninja
Last active December 11, 2015 22:19
Show Gist options
  • Save sweatpantsninja/4669004 to your computer and use it in GitHub Desktop.
Save sweatpantsninja/4669004 to your computer and use it in GitHub Desktop.
class OrderItem < ActiveRecord::Base
belongs_to :item
after_update :adjust_inventory, :if => lambda {
(size_changed? || item_id_changed?) && !returned?
}
def adjust_inventory
if item_id_changed?
Item.find(item_id_was).increment_stock(size_was)
item.reload.decrement_stock(size)
else
item.increment_stock(size_was)
item.decrement_stock(size)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment