Ruby on rails Rails ActiveRecord中用于查询的Arel节点与子查询的正确组合

Ruby on rails Rails ActiveRecord中用于查询的Arel节点与子查询的正确组合,ruby-on-rails,postgresql,arel,Ruby On Rails,Postgresql,Arel,我正在尝试使用Arel实现下面带有子查询的SQL students.classroom_id IN ( SELECT id FROM classrooms WHERE name IN ('foo', 'bar') ) 我试过下面的组合 Student.arel_table[:classroom_id].in( Classroom.where( Classroom.arel_table[:name].in( ['foo', 'bar'] ) ).select(

我正在尝试使用Arel实现下面带有子查询的SQL

students.classroom_id IN ( SELECT id FROM classrooms WHERE name IN ('foo', 'bar') )
我试过下面的组合

Student.arel_table[:classroom_id].in(
  Classroom.where(
    Classroom.arel_table[:name].in(
      ['foo', 'bar']
    )
  ).select(:id)
)
但是当我在上面执行
.to_sql
时,它返回

students.classroom_id IN ( NULL NULL NULL NULL )

任何帮助都将不胜感激。

你确定没有Arel就无法实现这一目标吗

Student.where(classroom: Classroom.where(name: ['foo', 'bar']))

我使用FF组合实现了我的目标输出:

subquery = Arel::Nodes::SqlLiteral.new Classroom.where(name: ['foo', 'bar'].select(:id).to_sql

Student.arel_table[:classroom_id].in(subquery)

这只是查询的一部分,我需要在SQL之后链接一个“or”语句,所以我的目标是使用Arel。