I know you can limit the returned fields in a Rails query by doing:
c = Client.select(:id, :user_id, :location_id).where(name: "John Doe")
=> #<ActiveRecord::Relation [#<Client id: 349, user_id: 348, location_id: 2>]>
but if you can only access the model through an association, how can you accomplish the same? (in this example Client belongs_to :user)
User.find_by(email: "[email protected]").client # I only want the 3 fields above
You can do it for example using scope, instead of association method:
or even better, joining
userand querying directly over users.email column: