0

因此,我试图在分配和总帐帐户之间建立关系,事情变得比我初步想象的要复杂一些,我只是想知道是否有更多优雅的方式多个has_one和has_many关系

因此,基本上一个分配必须有一个信用卡和借记帐户,两者都是总帐帐户,我不能使用STI,因为总帐帐户可以是一些分配的借方帐户,其他。

现在,我想要的另一个功能是查询特定总帐帐户上的所有借记和贷项。因此,总帐科目现在必须有许多贷方和借方交易。这是我到目前为止:

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 

我觉得应该有一个更简单的方法......任何人都可以称重吗?提前致谢!

回答

0

你在想什么:“分配”是否既可以是“借方”又可以是“贷方”?

如果这是不可能,您可以定义下一个型号:

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 
+0

分配必须是在信贷两个借记。在会计中,必须有一个交易从中取得的账户和一个交易进入的账户 –