Search code examples
ruby-on-railsdevisedevise-confirmable

Failed to update email address using devise only in production environment


This one is driving me absolutely crazy. I tested updating user's email address on dev and staging environments, and it all worked fine. It updates unconfirmed_email field and sends out a confirmation email address.

However, only in production environment, it fails!

In dev/staging environment, I see the following statements when a user updates his/her email address.

User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
Profile Load (0.3ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 2 LIMIT 1
(0.1ms)  BEGIN
User Exists (0.6ms)  SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]' AND "users"."id" != 2) LIMIT 1
(0.3ms)  UPDATE "users" SET "unconfirmed_email" = '[email protected]', "updated_at" = '2012-08-27 04:22:10.470329' WHERE "users"."id" = 2
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'jUcfXqMqDjeEsJ3TEKws' LIMIT 1
(0.3ms)  UPDATE "users" SET "unconfirmed_email" = '[email protected]', "updated_at" = '2012-08-27 04:22:10.470329', "confirmation_token" = 'jUcfXqMqDjeEsJ3TEKws', "confirmation_sent_at" = '2012-08-27 04:22:10.473264' WHERE "users"."id" = 2

However, in production, I see the following.

User Load (6.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
Profile Load (2.1ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 5 LIMIT 1
 (1.2ms)  BEGIN
User Exists (1.3ms)  SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]' AND "users"."id" != 5) LIMIT 1
 (1.2ms)  ROLLBACK

One more odd thing. I could update the email address manually in rails console.....

1.9.3p194 :005 > u.update_attributes(:email => "[email protected]")
(1.3ms)  BEGIN
User Exists (1.4ms)  SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]' AND "users"."id" != 1) LIMIT 1
(1.4ms)  UPDATE "users" SET "unconfirmed_email" = '[email protected]', "updated_at" = '2012-08-27 05:05:26.337961' WHERE "users"."id" = 1
User Load (1.4ms)  SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'Yyg98zno81adJt4mp7pG' LIMIT 1
(1.4ms)  UPDATE "users" SET "unconfirmed_email" = '[email protected]', "updated_at" = '2012-08-27 05:05:26.337961', "confirmation_token" = 'Yyg98zno81adJt4mp7pG', "confirmation_sent_at" = '2012-08-27 05:05:26.342299' WHERE "users"."id" = 1
Profile Load (1.4ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 1 LIMIT 1
(1.7ms)  COMMIT
=> true 

Any help would be much appreciated!

Thanks,


Solution

  • I was trying to figure out what the differences are between the two environments - dev/staging vs. production. Since staging and production have pretty much identical settings, the only I could think of was cacheing. So, I changed the update_attributes line to the following and it worked.

      if @user.reload.update_attributes(:email => params[:email])