Files
old-roraccounting/app/models/homie.rb

16 lines
580 B
Ruby
Raw Normal View History

2019-06-20 22:47:39 +02:00
class Homie < ApplicationRecord
2019-06-21 20:01:43 +02:00
has_many :money_moves_from, class_name: 'MoneyMove', foreign_key: :from_homie_id
has_many :money_moves_to, class_name: 'MoneyMove', foreign_key: :to_homie_id
2019-06-22 06:17:48 +02:00
def self.cash(importance)
got = Homie.all.joins(:money_moves_to).group(:id).order(:id).sum(:amount)
owe = Homie.all.joins(:money_moves_from).group(:id).order(:id).sum(:amount)
result = []
Homie.where(["importance > ?", importance]).map do |homie|
total = got.fetch(homie.id, 0) - owe.fetch(homie.id,0)
{ homie: homie, amount: total }
end
end
2019-06-20 22:47:39 +02:00
end