tab/app/models/transaction.rb

32 lines
762 B
Ruby
Raw Normal View History

2015-09-08 09:44:40 +00:00
# == Schema Information
#
# Table name: transactions
#
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# amount :integer default(0), not null
# origin :string not null
# message :string
# created_at :datetime not null
# updated_at :datetime not null
#
2015-09-08 09:30:11 +00:00
class Transaction < ActiveRecord::Base
2015-09-08 10:11:48 +00:00
belongs_to :debtor, class_name: 'User'
belongs_to :creditor, class_name: 'User'
2015-09-08 11:09:34 +00:00
after_save :recalculate_balances
after_destroy :recalculate_balances
2015-09-08 10:11:48 +00:00
def client
Client.find_by name: origin
end
2015-09-08 11:09:34 +00:00
private
def recalculate_balances
creditor.calculate_balance!
debtor.calculate_balance!
end
2015-09-08 09:30:11 +00:00
end