Raskolnikov
Raskolnikov

Reputation: 296

Rails Activerecord query selective include

I am having trouble optimizing a large activerecord query. I need to include an associated model in my request but due to the size of the return set I only want to include a couple of the associated columns. For example I have:

Post.includes(:user).large_set

While I am looking for something like:

Post.includes(:user.name, :user.profile_pic).large_set

I need to actually use the name and profile pic attributes so Post.joins(:user) is not an option as far as I understand.

Upvotes: 2

Views: 279

Answers (2)

user1454117
user1454117

Reputation:

select is what you are looking for:

Post.select("posts.*, users.name, users.profile_pic").large_set

http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

Upvotes: 2

TheIrishGuy
TheIrishGuy

Reputation: 2573

You'll have to use join to accomplish what you want, as includes does not have this functionality. Or you could white your own includes method :-)

Upvotes: 0

Related Questions