Rails:用支付的金额更新贷款金额

Rails: Update loan amount with amount paid

我是 rails 的新手,我写了一个跟踪贷款的小应用程序。有一个用户模型和一个贷款模型,我已经解决了跟踪谁借给谁钱等问题。

我正在尝试编写一个新功能来跟踪付款,但我不确定应该把它放在哪里。此功能将使用新值更新贷款金额的数据库值。此功能是否会成为贷款 class 的一部分,是否会成为可以访问贷款/用户模型的新控制器?

如果这个问题过于模糊,我深表歉意,如果需要,我可以提供更多信息。

您可以在贷款模型中创建一个方法 make_payment(amount) 并从贷款总额中减少金额。

我建议您保留一份付款记录,以便日后跟踪。创建属于 Loan 模型的 Payment 模型。

当您获得一笔不小的贷款时,常见的情况是您获得了分期付款的付款计划。

每期可分多次付款。

那么让我们来看看模型的外观:

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