Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Last active June 1, 2017 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckyruby/9b7c14694b95e60b8a3e477604f17f47 to your computer and use it in GitHub Desktop.
Save luckyruby/9b7c14694b95e60b8a3e477604f17f47 to your computer and use it in GitHub Desktop.
before_save :adjust_parts_qty
before_destroy :adjust_parts_qty
private
def adjust_parts_qty
old_quantity = quantity_was || 0
new_quantity = marked_for_destruction? ? 0 : quantity
part.quantity += new_quantity - old_quantity
part.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment