I have these two models user:
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
and userprofile:
class Userprofile < ActiveRecord::Base
belongs_to :user
attr_accessible :age, :fname, :gender, :lname, :photo_url
end
I’d like to check if there’s a userprofile object associated to the user. If there’s one, display it. Otherwise, create a new one.
I’m trying this and getting an error.
def show
@userprofile = current_user.userprofiles.all.where(:user_id => current_user.uid)
if !@userprofile.nil? then
@userprofile
else
@userprofile = Userprofile.new
end
end
undefined method `userprofiles’ for #
I’ve tried find with no better result.
user and userprofile have one-to-one relationship so that
by using this you can get userprofile of current_user
now your
showmethod look likeUpdate : why build
http://edgeguides.rubyonrails.org/association_basics.html#has-one-association-reference
we use
build_userprofilebecause it’sone-to-onerelation. but suppose if it’s has_many relationship then we useuserprofiles_build