我有这两个模型用户:
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
和用户资料:
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 !@userprofile.nil? then
@userprofile
else
@userprofile = Userprofile.new
end
end
未定义的方法 `userprofiles' 用于#
我试过find没有更好的结果。