0

Maintenance HABTM MaintenanceOrders

Given an array of MaintenanceOrder ids: [1,2,3,4,5], how can I retrieve all the Maintenances associated with those MaintenanceOrders?

Tried this:

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

But it rightly failed because maintenance_order_id isn't a column on Maintenance, and of course that doesn't make sense because it's a HABTM relationship...

This is the join table from schema:

create_table "maintenance_orders_maintenances", id: false, force: true do |t|
  t.integer "maintenance_order_id"
  t.integer "maintenance_id"
end
potashin
  • 44,205
  • 11
  • 83
  • 107
james
  • 3,989
  • 8
  • 47
  • 102

1 Answers1

0

Using includes, for example:

Maintenance.includes(:maintenance_orders).where(maintenance_orders: {id: [1,2,3,4,5]})
potashin
  • 44,205
  • 11
  • 83
  • 107