Files
old-roraccounting/app/models/homie.rb
2020-09-19 04:46:17 +03:00

29 lines
652 B
Ruby

class Homie < ApplicationRecord
has_many :money_moves
has_many :work
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