Files
old-roraccounting/app/models/homie.rb
2020-09-17 15:00:55 +03:00

28 lines
635 B
Ruby

class Homie < ApplicationRecord
has_many :money_moves
belongs_to :chip
def self.cash(importance)
totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
Homie.where(["importance > ?", importance]).map do |homie|
total = totals.fetch(homie.id, 0)
{ homie: homie, amount: total }
end
end
def settle
total = MoneyMove.where(homie_id: id).all.sum(:amount)
all_money_moves = MoneyMove.where(homie_id: id).all
transaction do
all_money_moves.destroy_all
MoneyMove.create_settle_record(id, total)
end
true
rescue StandardError => e
false
end
end