16 lines
580 B
Ruby
16 lines
580 B
Ruby
class Homie < ApplicationRecord
|
|
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
|
|
|
|
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
|
|
end
|