ActiveRecord 检索属于关联记录数组的 HABTM 关系中的记录

ActiveRecord retrieve records in HABTM relationship that belong to array of associated records

Maintenance HABTM MaintenanceOrders

给定一个 MaintenanceOrder id 数组:[1,2,3,4,5],我如何检索与那些 MaintenanceOrders 关联的所有 Maintenances

试过这个:

@paired_things = Maintenance.where(maintenance_order_id: [1,2,3,4,5])

但它确实失败了,因为 maintenance_order_id 不是关于维护的专栏,当然这没有意义,因为它是 HABTM 关系...

这是来自架构的联接 table:

create_table "maintenance_orders_maintenances", id: false, force: true do |t|
  t.integer "maintenance_order_id"
  t.integer "maintenance_id"
end

使用includes,例如:

Maintenance.includes(:maintenance_orders).where(maintenance_orders: {id: [1,2,3,4,5]})