Skip to content

Instantly share code, notes, and snippets.

@irridescentrambler
Last active June 27, 2019 13:07
Show Gist options
  • Save irridescentrambler/f5b4759fbf2b86da72c5306398635508 to your computer and use it in GitHub Desktop.
Save irridescentrambler/f5b4759fbf2b86da72c5306398635508 to your computer and use it in GitHub Desktop.
# == 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