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

Ruby on-RailsDevise

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

If you just want to prevent sending the email, you can use #skip_confirmation_notification, like so:

user = User.new(your, args)
user.skip_confirmation_notification!
user.save!

See documentation

> Skips sending the confirmation/reconfirmation notification email > after_create/after_update. Unlike #skip_confirmation!, record still > requires confirmation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionxjq233p_1View Question on Stackoverflow
Solution 1 - Ruby on-Railsnumbers1311407View Answer on Stackoverflow
Solution 2 - Ruby on-RailsBenjamin CrouzierView Answer on Stackoverflow