Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 多个有一个和多个关系_Ruby On Rails_Ruby On Rails 4_Activerecord_Associations_Has Many - Fatal编程技术网

Ruby on rails 多个有一个和多个关系

Ruby on rails 多个有一个和多个关系,ruby-on-rails,ruby-on-rails-4,activerecord,associations,has-many,Ruby On Rails,Ruby On Rails 4,Activerecord,Associations,Has Many,所以我试图在分配和总账账户之间建立一种关系,结果比我最初想象的要复杂一些,我只是想知道是否有一种更优雅的方式 所以基本上一个分配必须有一个贷方和借方账户,这两个账户都是总账账户,我不能使用STI,因为总账账户可以是某些分配的借方账户,但也可以是其他分配的贷方账户 现在,我想拥有的另一个功能是查询特定总账账户上的所有借项和贷项。因此,总分类账账户现在必须有许多贷记和借记交易。这就是我到目前为止所做的: class Allocation belongs_to :journal_entry_ite

所以我试图在分配和总账账户之间建立一种关系,结果比我最初想象的要复杂一些,我只是想知道是否有一种更优雅的方式

所以基本上一个分配必须有一个贷方和借方账户,这两个账户都是总账账户,我不能使用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

我觉得应该有一个更简单的方法。。。有人能称一下体重吗?提前谢谢

你怎么想:“分配”是否既可以是“借方”也可以是“贷方”

如果不可能,您可以定义下一个模型:

 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

分配必须是借方和贷方。在会计中,必须有一个账户,交易从该账户取得,交易进入该账户