我对rails非常陌生,我写了一个跟踪贷款的小应用程序。有一个用户模型和一个贷款模型,我已经找到了跟踪谁借钱谁等等。
我正在尝试编写一个可以跟踪付款的新功能,但我不确定应该放在哪里。此功能将使用新值更新贷款金额的数据库值。这个功能是贷款类的一部分吗,它是一个可以访问贷款/用户模型的新控制器吗?
如果这个问题过于含糊,我道歉,我可以提供更多信息。
答案 0 :(得分:1)
您可以在贷款模型make_payment(amount)
中创建一个方法,并从贷款总额中减少金额。
我建议您维护付款记录,以便日后跟踪。创建属于Payment
模型的Loan
模型。
答案 1 :(得分:1)
当您为非重要金额贷款时,常见的情况是您获得的付款计划将以分期付款的方式支付。
每期付款都可以通过多笔付款支付。
让我们来看看模型的样子:
class User < ActiveRecord::Base
has_many :loans
has_many :instalments, through: :loans
has_many :payments, through: :loans
end
# [int, foreign_key, index] user_id
# [float] total_amount - does not change!
class Loan < ActiveRecord::Base
belongs_to :user
has_many :instalments
has_many :payments
end
# [int, foreign_key, index] loan_id
# [float] total_due
# [float] payed
# [datetime] due_date
class Instalment < ActiveRecord::Base
belongs_to :loan
has_one :user, through: :loan
has_many :payments
end
# [int, foreign_key, index] instalment_id
# [float] amount
class Payment < ActiveRecord::Base
belongs_to :instalment
has_one :loan, through: :instalment
has_one :user, through: :loan
end
这可能看起来过于复杂,但实际上足以产生某种财务责任。我们可以跟踪用户每月应支付的金额 - 以及哪些付款与特定分期付款相关联。如果你真的想真正使用这个应用程序,你很可能会遇到更多的法律责任要求(除非你的目标是瑞士市场)。
让我们在这里谈谈应用程序流程:
请注意,在此域模型中,我们不会在创建后更新贷款金额 - 它只是他们借入的原始金额的记录(请记住,出于会计目的,您应始终保留数据)
我们不必弄清楚如何保留贷款,而是将instalments.payed
列加起来:
# [int, foreign_key, index] user_id
# [float] total_amount - does not change!
class Loan < ActiveRecord::Base
belongs_to :user
has_many :instalments
has_many :payments
def payed
installments.sum(:payed)
end
def amount_due
total_amount - installments.sum(:payed)
end
end
这是您需要的路线:
resources :users, shallow: true do
resources :loans
end
resources :loans, only: [], shallow: true do
resources :instalments
end
resources :instalments, only: [], shallow: true do
resources :payments
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
user_loans GET /users/:user_id/loans(.:format) loans#index
POST /users/:user_id/loans(.:format) loans#create
new_user_loan GET /users/:user_id/loans/new(.:format) loans#new
edit_loan GET /loans/:id/edit(.:format) loans#edit
loan GET /loans/:id(.:format) loans#show
PATCH /loans/:id(.:format) loans#update
PUT /loans/:id(.:format) loans#update
DELETE /loans/:id(.:format) loans#destroy
loan_instalments GET /loans/:loan_id/instalments(.:format) instalments#index
POST /loans/:loan_id/instalments(.:format) instalments#create
new_loan_instalment GET /loans/:loan_id/instalments/new(.:format) instalments#new
edit_instalment GET /instalments/:id/edit(.:format) instalments#edit
instalment GET /instalments/:id(.:format) instalments#show
PATCH /instalments/:id(.:format) instalments#update
PUT /instalments/:id(.:format) instalments#update
DELETE /instalments/:id(.:format) instalments#destroy
instalment_payments GET /instalments/:instalment_id/payments(.:format) payments#index
POST /instalments/:instalment_id/payments(.:format) payments#create
new_instalment_payment GET /instalments/:instalment_id/payments/new(.:format) payments#new
edit_payment GET /payments/:id/edit(.:format) payments#edit
payment GET /payments/:id(.:format) payments#show
PATCH /payments/:id(.:format) payments#update
PUT /payments/:id(.:format) payments#update
DELETE /payments/:id(.:format) payments#destroy