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

分配必須是在信貸兩個借記。在會計中,必須有一個交易從中取得的賬戶和一個交易進入的賬戶 –