# Working example Project.joins('INNER JOIN "users" AS "students" ON "students"."project_id" = "projects"."id" AND "students"."type" IN ("Student")').order('students.name DESC') # Try to transform query to Arel s = Arel::Table.new(:users, as: 'students') Project.joins(p.join(s).join_sources) Project.joins(Project.arel_table.join(Student.arel_table).on(Student.arel_table[:project_id].eq(Project.arel_table[:id]).and(Student.arel_table[:type].in('Student'))).join_sources).to_sql p = Arel::Table.new(:projects) s = Arel::Table.new(:users, as: 'students') Project.joins(p.join(s).on(p[:id].eq(s[:project_id]).and(s[:type].in('Student'))).join_sources)