我有這兩款車型用戶:如何在一對一關係中查找對象模型?
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,因爲有很多關於我們不知道的關聯的東西。 –