Reputation: 3264
I have user
model which has_one
association with user_profile
And I want to select name field from user_profile instead of user_profile
.*
I have tried,
user = User.first
user.user_profile.select(:name)
But this is not working.any way?
Upvotes: 0
Views: 1041
Reputation: 13925
UPDATED:
It seems, that rails handles the other direction and one-to-one connections differently. You have two options:
1) Define the selected attributes in the association, it will always select those
has_one :user_profile, :select => [:name, :id]
2) Define a specific find in your model, where you can add select
, like this:
def my_profile
UserProfile.find(self.user_profile_id)
end
....
my_profile.select(:name)
ORIGINAL:
In case of has_many
direction, it works:
I've tried your method in my code, like:
= User.find("admin").article.select(:title).to_sql
It returns the correct sql:
SELECT title
FROM "articles"
WHERE "articles"."user_id" = 1364
Upvotes: 1