views:

36

answers:

1

I've been looking for how to do a select with include in Rails 3 - I have a nested example:

@items = Item.where("fulfilled = ?", true).includes({:order=>[:supplier, :agent]}, :manufacturer)

This is a very taxing query to run without selects as it creates 1000s of rows of data by pulling information from all the above big tables. How can I get the query to only select these fields?

  • user.name, user.created_at
  • order.created_at
  • supplier.name
  • agent.name
  • manufacturer.name
A: 

There is a select method in ARel, but you must use the correct table names (i.e. plural and beware if you have polymorphic models or if you're using set_table_name or some other similar non-standard practice)

@items = Item.select('users.name, users.created_at, orders.created_at, suppliers.name, agents.name, manufacturers.name).where("fulfilled = ?", true).includes({:order=>[:supplier, :agent]}, :manufacturer)
Patrick Klingemann