我有user
模型,has_one
与user_profile
HAS_ONE协会选择特定领域
协会,我想从user_profile而不是user_profile
选择名称字段。*
我都试过了,
user = User.first
user.user_profile.select(:name)
但是这不起作用。
我有user
模型,has_one
与user_profile
HAS_ONE协会选择特定领域
协会,我想从user_profile而不是user_profile
选择名称字段。*
我都试过了,
user = User.first
user.user_profile.select(:name)
但是这不起作用。
更新:
看来,该轨道处理另一个方向和一个一对一的连接方式不同。你有两个选择:
1)定义在协会选定的属性,它总是会选择那些
has_one :user_profile, :select => [:name, :id]
2)定义在模型中,一个特定的查找,您可以添加select
,像这样的:
def my_profile
UserProfile.find(self.user_profile_id)
end
....
my_profile.select(:name)
ORIGINAL:
在has_many
方向的情况下,它的工作原理:
我已经试过你的方法在我的代码,如:
= User.find("admin").article.select(:title).to_sql
它返回正确的SQL:
SELECT title
FROM "articles"
WHERE "articles"."user_id" = 1364
我使用Rails 3.2 .5 – shajin 2012-07-23 12:16:44