Devise - create user account with confirmed without sending out an email?

disappearedng picture disappearedng · Sep 19, 2011 · Viewed 32.8k times · Source

I integrated devise with facebook. Now when I create a user account after the user has logged in with his/her facebook account,

  user = User.create(:email => data["email"], 
                     :password => Devise.friendly_token[0,20]) 
  user.confirmed_at = DateTime.now
  user.save!

even though the account has been confirmed, an confirmation email is still fired. Any idea how I can turn the email firing off?

Answer

numbers1311407 picture numbers1311407 · Sep 19, 2011

The confirm callback happens after create, so it's happening on line 1 of your example, before you set confirmed_at manually.

As per the comments, the most correct thing to do would be to use the method provided for this purpose, #skip_confirmation!. Setting confirmed_at manually will work, but it circumvents the provided API, which is something which should be avoided when possible.

So, something like:

user = User.new(user_attrs)
user.skip_confirmation!
user.save!

Original answer:

If you pass the confirmed_at along with your create arguments, the mail should not be sent, as the test of whether or not an account is already "confirmed" is to look at whether or not that date is set.

User.create(
  :email => data['email'], 
  :password => Devise.friendly_token[0,20], 
  :confirmed_at => DateTime.now
)

That, or just use new instead of create to build your user record.