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})