ActiveRecord includes. Specify included columns

Rails doesn’t have the facility to pass the options for include query. But we can pass these params with the association declaration under the model.

For your scenario, you need to create a new association with users model under the profile model, like below

belongs_to :user_only_fetch_email, :select => "users.id, users.email", :class_name => "User"

I just created one more association but it points to User model only. So your query will be,

Profile.includes(:user_only_fetch_email)

or

Profile.includes(:user_only_fetch_email).find(some_profile_ids)

Leave a Comment