Skip to content

Instantly share code, notes, and snippets.

@Harrisonl
Created February 12, 2015 11:42
Show Gist options
  • Save Harrisonl/59adece0d44201c209dc to your computer and use it in GitHub Desktop.
Save Harrisonl/59adece0d44201c209dc to your computer and use it in GitHub Desktop.
######app/models/debt.rb
#Calculates how long to pay off each debt in order of highest interest to lowest
def self.payoff_highest_interest(debts)
balance = debts.sort{|a, b| b.interest <=> a.interest}.first.balance
date = Date.current.beginning_of_month
debts.reduce(0) do |acc, debt|
while debt.balance > 0
date = date + 1.month
acc + 1
adj_bal = balance + debt.compound_interest
if debt.user.payments.map(&:expected_on).include?(date) ## ERROR OCCURS HERE I THINK
debt.balance = adj_bal - (calculating_debt_payment(debts) + debt.minimum_payment + debt.user.payments.where(:expected_on => date))
else
5.00 ## IGNORE THIS PART
end
end
end
end
######Server log
Started GET "/users/1" for 127.0.0.1 at 2015-02-12 22:37:02 +1100
Processing by UsersController#show as HTML
Parameters: {"id"=>"1"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Debt Load (15.3ms) SELECT "debts".* FROM "debts" WHERE "debts"."user_id" = $1 [["user_id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "debts".* FROM "debts" WHERE "debts"."user_id" = $1 [["user_id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "debts".* FROM "debts" WHERE "debts"."user_id" = $1 [["user_id", 1]]
Payment Load (17.4ms) SELECT "payments".* FROM "payments" WHERE "payments"."user_id" = $1 [["user_id", 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment