Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 RubyonRails-多对一类型关联_Ruby On Rails_Associations_Has Many_Relationships_Many To One - Fatal编程技术网

Ruby on rails RubyonRails-多对一类型关联

Ruby on rails RubyonRails-多对一类型关联,ruby-on-rails,associations,has-many,relationships,many-to-one,Ruby On Rails,Associations,Has Many,Relationships,Many To One,我正在尝试创建某种多对一的关联。基本前提是一个货币/交易流系统,用于跟踪用户两个账户(可能是钱包和支票账户)之间的货币 我有一个交易模型,它存储基本信息——借记哪个账户,贷记哪个账户,金额是多少 我还有一个账户模型,用户可以创建多个账户(可能一个用于钱包,一个用于信用卡,一个用于支票账户,等等) 我想我遇到的问题是,我的交易模型引用了账户模型两次,一次是贷方id,一次是借方id 我正试图找出我需要的关联,我想我需要多对一(多笔交易,一个账户)。我不认为我需要一个联接表,但我不能完全确定 这是我的

我正在尝试创建某种多对一的关联。基本前提是一个货币/交易流系统,用于跟踪用户两个账户(可能是钱包和支票账户)之间的货币

我有一个交易模型,它存储基本信息——借记哪个账户,贷记哪个账户,金额是多少

我还有一个账户模型,用户可以创建多个账户(可能一个用于钱包,一个用于信用卡,一个用于支票账户,等等)

我想我遇到的问题是,我的交易模型引用了账户模型两次,一次是贷方id,一次是借方id

我正试图找出我需要的关联,我想我需要多对一(多笔交易,一个账户)。我不认为我需要一个联接表,但我不能完全确定

这是我的基本模型代码,我真的不知道从这里走到哪里

class Transaction < ActiveRecord::Base
  attr_accessible :amount, :description, :credit_id, :debit_id

  belongs_to :user

  belongs_to :debit, :class_name => "Account"
  belongs_to :credit, :class_name => "Account"


end
类事务“账户”
属于:贷方,:class\u name=>“账户”
终止
然后对于帐户模型:

class Account < ActiveRecord::Base
  attr_accessible :name, :credit_transactions, :debit_transactions

  belongs_to :user

  has_many :transactions
  has_many :credit_transactions, :through => :transactions, :source => :credit
  has_many :debit_transactions, :through => :transactions, :source => :debit
end
类帐户:交易,:来源=>:信用
有多个:借方交易,:至=>:交易,:来源=>:借方
终止
通过此模型实现,我可以正确地获取transaction.credit和transaction.debit,但当我尝试执行类似account.credit\u transactions的操作时,会出现以下错误:

ActiveRecord::StatementInvalid:SQLite3::SQLException:没有这样的列:transactions.account\u id:选择“accounts”。*从“accounts”内部连接“accounts”上的“transactions”。id=“transactions”。debit\u id其中((“transactions.account\u id=3))

老实说,我有点纠结于下一步该去哪里,所以任何帮助都将不胜感激。谢谢


[编辑:更新型号代码]

听上去您的交易有一个信用账户和一个借记账户

class Transaction < ActiveRecord::Base

  has_one :credit_account, :class_name => "Account", :foreign_key => "credit_account_id"
  has_one :debit_account, :class_name => "Account", :foreign_key => "debit_account_id"

end
类事务“账户”,:外键=>“信用卡账户id”
有一个:借方账户,:类名称=>“账户”,:外键=>“借方账户id”
终止

如果这不起作用,你能提供一些关于模型之间关系应该如何工作的更多信息吗?

很抱歉耽搁了,但最终解决了这个问题。这是我的密码

    class Transaction < ActiveRecord::Base
      attr_accessible :amount, :description, :long_description, :credited_id, :debitted_id, :custom_credit, :custom_debit

      belongs_to :user

      belongs_to :debitted, :class_name => "Account"
      belongs_to :credited, :class_name => "Account"
类事务“账户”
属于:贷方,:class\u name=>“账户”
而且

    class Account < ActiveRecord::Base
      attr_accessible :name, :debit_shorthand, :credit_shorthand, :credit_transactions, :debit_transactions

      belongs_to :user

      has_many :credit_transactions, :class_name => "Transaction", :foreign_key => 'credited_id'
      has_many :debit_transactions, :class_name => "Transaction", :foreign_key => 'debitted_id'
类帐户“Transaction”,:foreign\u key=>“credited\u id”
有很多:借记交易,:类名称=>“交易”,:外键=>“借记交易id”

谢谢所有帮忙的人

你很少会使用一个有这样一个关联的。。。也许更正确的说法是,交易属于信用账户,而属于借记账户!实际上,我对代码做了一些修改,并采纳了您的建议。当我尝试进行transaction.credit和transaction.debit时,我可以得到正确的帐户模型。然而,我不能做相反的事情;例如,账户、贷记交易或账户、借记交易。我得到错误“ActiveRecord::StatementInvalid:SQLite3::SQLException:没有这样的列:transactions.account\u id:选择“accounts”。*从“accounts”内部加入“accounts.id=”transactions.debit\u id WHERE((“transactions.account\u id=3));有什么帮助吗?哈哈,我想这是行不通的。使用has\u one,交易的外键将在帐户上,但一个帐户可以有许多交易。根据其发音,贷记帐户和借记帐户应该在交易上