0

I followed Ryan Bates #235 Devise and OmniAuth (revised) http://railscasts.com/episodes/235-devise-and-omniauth-revised and was able to make it work.

Now, I would like to add a Profile to the User.

So, I tried this...

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = auth.info.nickname
      user.profile = user.build_profile
      user.profile.name = auth.info.name
      user.profile.save
    end
  end

At first glance, everything seemed ok. But I noticed that it didn't update the "name" attribute.

1.9.3p125 :019 > Profile.last
  Profile Load (0.4ms)  SELECT "profiles".* FROM "profiles" ORDER BY "profiles"."id" DESC LIMIT 1
 => #<Profile id: 8, user_id: 3, name: nil, created_at: "2012-08-18 06:00:59", updated_at: "2012-08-18 06:00:59">

So I tried to set a value, this is what happens:

 1.9.3p125 :020 > p=Profile.last
  Profile Load (0.4ms)  SELECT "profiles".* FROM "profiles" ORDER BY "profiles"."id" DESC LIMIT 1
 => #<Profile id: 8, user_id: 3, name: nil, created_at: "2012-08-18 06:00:59", updated_at: "2012-08-18 06:00:59">
1.9.3p125 :021 > p.name = "Adam"
 => "Adam" 
1.9.3p125 :022 > p.save
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
 => true 
1.9.3p125 :023 > p.name
 => "Adam" 
1.9.3p125 :024 > p
  => #<Profile id: 8, user_id: 3, name: nil, created_at: "2012-08-18 06:00:59", updated_at: "2012-08-18 06:00:59">

I was expecting that the name would be updated. What should I have done?

Thanks

Fred Guth
  • 1,537
  • 1
  • 15
  • 27

2 Answers2

1

Try:

 accepts_nested_attributes_for :profile

in your User model

Han
  • 1,283
  • 11
  • 25
0

I think I found out what was the problem. "name" is a reserved attribute. I changed it to "owner_name" and now p.owner_name="Adam" works. I didn't know I shouldn't use "name" as an attribute.

Thank you all!

Fred Guth
  • 1,537
  • 1
  • 15
  • 27