Rails:更新贷款金额与支付金额

我很新的轨道,我写了一个小应用程序,跟踪贷款。有一个用户模型和贷款模型,我有它的工作,以追踪谁贷款谁是钱等。Rails:更新贷款金额与支付金额

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

我很抱歉,如果这个问题过于模糊,我可以提供更多的信息是必要的。

回答:

您可以在贷款模式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

以上是 Rails:更新贷款金额与支付金额 的全部内容, 来源链接: utcz.com/qa/260949.html

回到顶部