Last active
June 27, 2019 13:07
-
-
Save irridescentrambler/f5b4759fbf2b86da72c5306398635508 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: accounts | |
# | |
# id :bigint not null, primary key | |
# balance :integer | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
class Account < ApplicationRecord | |
validate :account_balance_non_zero? | |
def deposit(amount) | |
self.balance += amount | |
save! | |
end | |
def withdraw(amount) | |
self.balance -= amount | |
save! | |
end | |
private | |
def account_balance_non_zero? | |
return true if balance >= 0 | |
errors.add(:balance, "Insufficient") | |
false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment