When I create a new account through google, the email gets stored in my user record. When I create a user through twitter, the email column is blank. I’d like to update that entry if a user associates their current twitter account with google.
in my User model:
def self.create_from_hash!(hash)
create! do |user|
user.name = hash['user_info']['name']
user.email = hash['user_info']['email']
end
end
In sessions controller:
def create
auth = request.env['rack.auth']
unless @auth = Authorization.find_from_hash(auth)
@auth = Authorization.create_from_hash(auth, current_user)
end
self.current_user = @auth.user
flash[:notice] = "Welcome, #{current_user.name}."
redirect_to '/'
end
and in the Authorization model:
def self.create_from_hash(hash, user = nil) user ||= User.create_from_hash!(hash) Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider']) end
How can I update that column when I am adding an authorization method?
I added the following line into my sessionscontroller create action, which seems to solve the issue:
if @user && (@user.email.blank? || @user.email.nil?) @user.update_attribute(:email, request.env['rack.auth']['user_info']['email']) unless request.env['rack.auth']['user_info']['email'].nil? || @user.nil? end