Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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
Sql Rails查询-如何连接特定列上的两个表_Sql_Ruby On Rails_Join - Fatal编程技术网

Sql Rails查询-如何连接特定列上的两个表

Sql Rails查询-如何连接特定列上的两个表,sql,ruby-on-rails,join,Sql,Ruby On Rails,Join,我有一个模型(所有权转让),其中有两列属于另一个模型(账户): class OwnershipTransfer < ApplicationRecord belongs_to :transfer_from, class_name: 'Account' belongs_to :transfer_to, class_name: 'Account' end 我尝试过:Account.joins(:ownership\u transfer).on(Account.arel\

我有一个模型(所有权转让),其中
有两列
属于另一个模型(账户)

  class OwnershipTransfer < ApplicationRecord

   belongs_to :transfer_from, class_name: 'Account'
   belongs_to :transfer_to, class_name: 'Account'

  end
我尝试过:
Account.joins(:ownership\u transfer).on(Account.arel\u table[:id].eq(OwnershipTransfer.arel\u table[:transfer\u from\u id])。其中(ownership\u transfers:{meeting\u event\u id:458})

但是在rails 6中,
不推荐将委托给arel

有什么线索可以明确地选择一个列来连接吗?

为了清晰起见,将各个部分分开

涉及的表格

account_table = Account.arel_table
transfership_table = OwnershipTransfer.arel_table
阿雷尔加入

join = account_table.join(transfership_table).on(account_table[id].eq( transfership_table[:transfer_from_id] ))
正在查询中使用arel联接

Account.joins(join.join_sources).where(ownership_transfers: {meeting_event_id: 458})

Arel是ActiveRecord的骨干,因此我强烈怀疑它是否在Rails 6中被弃用。您从何处获得此信息?事实并非如此,只有从关系加入源的委派是不推荐的。Arel join_源仍然存在。你们都是对的@max,@dbugger在上面运行查询,我有一个错误:
ActiveSupport::DepractionException(弃用警告:向arel授权已弃用,将在Rails 6.0中删除。
问题已修改。另一个选项是在
帐户中提供计数器关联,并跳过
arel
。例如
类帐户;有多个:从转移,类名称:'OwnershipTransfer',外键::从id转移;结束然后是
帐户。从
转账将产生相同的结果。
join = account_table.join(transfership_table).on(account_table[id].eq( transfership_table[:transfer_from_id] ))
Account.joins(join.join_sources).where(ownership_transfers: {meeting_event_id: 458})