[英]multiple has_one and has_many relationships
so I'm trying to make a relationship between an allocation and a general ledger account, and it's turning out to be a bit more complicated than I initally thought, I was just wondering if there was a more elegant way 所以我试图在分配和总账科目之间建立一种关系,结果比最初想像的要复杂一些,我只是想知道是否有更优雅的方法
So basically an allocation must have a credit and debit account, both of which are general ledger accounts, and I can't use STI because a general ledger account can be a debit account on some allocations but credit account on others. 因此,基本上一个分配必须有一个贷方和借方帐户,这两个帐户都是总分类帐帐户,我不能使用STI,因为总分类帐帐户可以是某些分配上的借方帐户,而可以是其他分配上的贷方帐户。
Now, another feature I want to have is to query all the debits and credits on a particular general ledger account. 现在,我要拥有的另一个功能是查询特定总账帐户上的所有借方和贷方。 So a general ledger account now must have many credits and debit transactions.
因此,总账帐户现在必须有许多贷方和借方交易。 This is what I have so far:
这是我到目前为止的内容:
class Allocation
belongs_to :journal_entry_item
belongs_to :allocatable, polymorphic: true
has_one :debit_allocation
has_one :credit_allocation
has_one :debit_account, through: :debit_allocation, source: :general_ledger_account
has_one :credit_account, through: :credit_allocation, source: :general_ledger_account
end
class DebitAllocation
belongs_to :allocation
belongs_to :general_ledger_account
end
class CreditAllocation
belongs_to :allocation
belongs_to :general_ledger_account
end
class GeneralLedgerAccount
belongs_to :company
has_many :debit_allocations
has_many :credit_allocations
has_many :debits, through: :debit_allocations, source: :allocation
has_many :credits, through: :credit_allocations, source: :allocation
end
I feel like there should be a simpler way... Can anyone weigh in? 我觉得应该有一个更简单的方法...有人可以称重吗? Thanks in advance!
提前致谢!
What You think about: whether 'Allocation' can be both 'debit' and 'credit'? 您的想法:“分配”既可以是“借方”又可以是“贷方”?
If it is not possible, You could define next Models: 如果不可能,则可以定义下一个模型:
class Allocation
belongs_to :journal_entry_item
belongs_to :general_ledger_account
has_one :general_ledger_account
has_one :debit_account, source: :general_ledger_account
has_one :credit_account, source: :general_ledger_account
def is_debit?
debit_account&&!credit_account
end
def is_credit?
!debit_account&&credit_account
end
end
class GeneralLedgerAccount
belongs_to :company
has_many :allocations
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.