I have a
user model that belongs to to an
account (pretty standard). On every request, I need to retrieve the
current_user but I also want to return the
account to reduce DB queries:
@current_user ||= User.includes(:account).find_by(auth_token: cookies[:auth_token])
@current_account ||= current_user.account
This works just fine. However, the
account record has a lot of extra text data that I don't always want to return. A more permanent solution would be to move those extra fields to a new table called
account_details and have a one-to-one between an
However, I need a shorter-term solution that will provide some optimizations until I can refactor that (perfection is an iterative process!).
I want to return ALL columns from the
current_user, but I only want to select a few columns from the
I tried this and I didn't work:
@current_user ||= User.includes(:account).references(:account).select("users.*, account.id, account.uuid").find_by(auth_token: cookies[:auth_token])
When I look at
@current_user.account it still displays all the columns with their values and I only wanted the
id, uuid columns.
I couldn't find any clear syntax on using
includes(:association); is this something that's possible?