我有这两款车型用户:如何在一对一关系中查找对象模型?
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
# This is a class method, callable from SessionsController
# hence the "User."
def User.create_with_omniauth(auth)
user = User.new()
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.save
return user
end
has_one :userprofile
end
和USERPROFILE:
class Userprofile < ActiveRecord::Base
belongs_to :user
attr_accessible :age, :fname, :gender, :lname, :photo_url
end
我想检查是否有与用户相关的一个USERPROFILE对象。如果有,显示它。否则,请创建一个新的。
我正在尝试这个,并得到一个错误。
def show
@userprofile = current_user.userprofiles.all.where(:user_id => current_user.uid)
if [email protected]? then
@userprofile
else
@userprofile = Userprofile.new
end
end
未定义的方法`的UserProfiles'为#
我已经试过找到没有更好的结果。
它正在工作。我只是想知道我该如何猜测build_userprofile方法的存在。大声笑!任何链接? – Richard77
看到更新:为什么建立 - 并看到链接 –
更好的链接将http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference,因为有很多关于我们不知道的关联的东西。 –