借方と貸方を管理する請求システムがあります。基本的に、請求額は借方の合計から得られ、残高は貸方の合計を合計額から差し引くことによって算出されます。
私はこれを4つのモデルで行っています。
- 請求書
- ラインアイテム
- デビット
- クレジット
それが機能する方法は、recordableと呼ばれるポリモーフィックな関連付けを持つ結合モデル(Line Item)を介したものです。一見、すべてが正しく機能しているように見えます。ただし、ラインアイテムを調べると、recordable_idは正常に表示されますが、recordable_typeはnilであることがわかります。
コードの内訳は次のとおりです。
class Invoice < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
has_many :debits, :through => :line_items, :as => :recordable
has_many :credits, :through => :line_items, :as => :recordable
end
class LineItem < ActiveRecord::Base
belongs_to :invoice
belongs_to :recordable, :polymorphic => true
belongs_to :credit, :class_name => "Credit", :foreign_key => "recordable_id"
belongs_to :debit, :class_name => "Debit", :foreign_key => "recordable_id"
end
class Credit < ActiveRecord::Base
has_many :line_items, :as => :recordable, :dependent => :destroy
end
class Debit < ActiveRecord::Base
has_many :line_items, :as => :recordable, :dependent => :destroy
end
誰かが私をここで正しい方向に向けることができますか?