Guest User

Untitled

a guest
Feb 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. class Item < ActiveRecord::Base
  2. has_many :basket_items
  3.  
  4. def withdraw(num)
  5. adjust_balance_and_save(-num)
  6. end
  7.  
  8. def deposit(num)
  9. adjust_balance_and_save(num)
  10. end
  11.  
  12. private
  13. def adjust_balance_and_save(num)
  14. self.quantity += num
  15. save!
  16. end
  17.  
  18. def validate
  19. errors.add(:quantity, "is negative") if quantity < 0
  20. end
  21. end
Add Comment
Please, Sign In to add comment