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(:users => { :fulfilled => true }).
includes(:orders => [:supplier, :agent], :manufacturer)
"We can use select with joins not includes" - @Bhavesh_A_P
Note:
As @Bhavesh_A_P pointed out above, select
with includes
does not produce consistent behavior. It appears that if the included association returns no results, select
will work properly, if it returns results, the select statement will have no effect. In fact, it will be completely ignored, such that your select statement could reference invalid table names and no error would be produced. select
with joins
will produce consistent behavior.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…