ruby - Rails class name/type not working for a polymorphic has_many :through -
I have an invoicing system that manages debits and credits. Invoice amount is originally received from the amount of its debit and the balance takes its credit and deducts it from the total amount.
I am doing it with four models.
By the way It works through an inserted model (line item), which is called a polymorphic association, which is called recordable. At first glance, everything seems to work properly However, inspecting the line object indicates that the recordable_ID looks fine, so the recordable_type is zero.
There is a section of code here:
class invoice & lt; ActiveRecord :: Base is - Medium: Line_Entems, Dependent = & gt; : Has_many deleted: debit, through: = & gt; : Line_tyme, as in: = & gt; : Is recordable_image: credits, through: = & gt; : Line_tyme, as in: = & gt; : Recordable End Class LineItem & lt; ActiveRecord :: Base is_to: Invoice is_to: recordable ,: polymorphic = & gt; The truth is included: credit, class_name = & gt; "Credit",: foreign_key => "Recordable_id" belong_to: debit ,: class_name = & gt; "Debit" ,: foreign_key => "Recordable_head" end class credit & lt; ActiveRecord :: Base has_many: line_items ,: as = & gt; : Recordable ,: Dependent = & gt; : Delete and Debit Debit & lt; ActiveRecord :: Base has_many: line_items ,: as = & gt; : Recordable ,: Dependent = & gt; Has anyone told me in the right direction?
You are unclear on your LineItem
class The organizations are declaring
In short, related_to
does this in your class:
-
related_to: invoice
a method Invoice
that searches for invoice table for records referenced by invoice_id
-
related_to: recordable, : Polygonity = & gt; Searches the recordable_type.underscore.pluralize
table for a record referenced by recordable_id
, which is a recordable
Li> related_to: credit,: class_name = & gt; "Credit",: foreign_key => Creates "Recordable_id"
a method credit
that searches through the credit table for records referenced by recordable_id
. Note that recordable_type
is ignored here - In this order, it applies to
related_to: debit
.
Since your line item has only a credit card or , it does not make any sense to declare these organizations in additional form. You can refer to them by the recordable
association.
Comments
Post a Comment