Rails supports loading with selected attributes

Can someone tell me if a desire can load an association but only return certain attributes?

I would like to receive some orders with my accounts, but you only need account names.

Order.select([:id, :account_id]).includes(:account).limit(2)

+5
source share
1 answer

I think jvnill comment says it all:

I am not sure if this is possible. However, you can add the account name to the records of the returned order.

orders = Order.joins(:account).select('orders.id, orders.account_id, accounts.name AS account_name')

then just use account_name, e.g. orders.first.account_name

+5
source

All Articles