简体   繁体   English

Ruby on Rails:在数据库中迁移用户时如何防止确认电子邮件?

[英]Ruby on Rails: How to prevent confirmation emails when migrating users in database?

In our app, we would like to migrate users from old table to a new one, managed by devise gem. 在我们的应用程序中,我们希望将用户从旧表迁移到由devise gem管理的新表。 I linked the two tables with an attribute old_id so I can always go back to the previous user information and get other data from there. 我用属性old_id链接了两个表,因此我总是可以返回到先前的用户信息并从那里获取其他数据。 Here is the migration script (once tables are created): 这是迁移脚本(一旦创建表):

class PopulateV3idInUsersTable < ActiveRecord::Migration

  def up
    User.all.each do |u|
      old_u = OldUser.find_by( email: u.email )
      unless old_u.nil?
        u.old_id = old_u.id
        u.skip_confirmation!
        u.save
      end
    end
  end

end

The database is correctly updated. 数据库已正确更新。 The problem is that, each time we update a user with his old id, the script sends a confirmation email to the user, which is really not good... 问题在于,每次我们使用旧ID更新用户时,脚本都会向该用户发送一封确认电子邮件,这确实不好。

In the previous code, I added u.skip_confirmation! 在前面的代码中,我添加了u.skip_confirmation! but it still doesn't work. 但它仍然不起作用。 I also tried other possibilities like u.confirm! 我也尝试过其他可能性,例如u.confirm! and u.confirmation_token = nil ; u.confirmed_at = Time.now u.confirmation_token = nil ; u.confirmed_at = Time.now u.confirmation_token = nil ; u.confirmed_at = Time.now but they all failed. u.confirmation_token = nil ; u.confirmed_at = Time.now但是它们都失败了。

Do you have an idea? 你有想法吗? Thanks. 谢谢。

Give this a try: 试试看:

class PopulateV3idInUsersTable < ActiveRecord::Migration
  def up
    User.all.each do |u|
      old_u = OldUser.find_by( email: u.email )
      unless old_u.nil?
        u.old_id = old_u.id
        u.skip_confirmation_notification!
        u.confirmation_sent_at = nil
        u.save
      end
    end
  end

As Luke mentioned, you want to use #skip_confirmation_notification! 正如卢克所说,您想使用#skip_confirmation_notification! rather than #skip_confirmation! 而不是#skip_confirmation! .

But this will only do the trick in your migration task. 但这只会解决您的迁移任务。 Next time an update occurs on the user (say, he wants to change his first name), the confirmation mail will be sent again. 下次用户发生更新(例如,他想更改其名字)时,将再次发送确认邮件。

If devise tries to send a confirmation email, that's because : 如果devise尝试发送确认电子邮件,那是因为:

  1. your User model includes :confirmable and 您的用户模型包括:confirmable
  2. your users has no confirmed_at attribute set 您的用户未设置confirmed_at属性

not using confirmable 不使用可确认的

If you don't want confirmation at all, remove :confirmable from devise options : 如果您根本不想确认,请从devise选项中删除:confirmable

class User < ActiveRecord::Base
  # whatever modules you want to use, except :confirmable
  devise :database_authenticatable, :registrable, :recoverable
end

using confirmable 使用可确认的

If you want to use confirmable but fix your old users problem, set their #confirmed_at attribute in your migration : 如果您想使用#confirmed_at但要解决旧用户问题,请在迁移中设置其#confirmed_at属性:

class PopulateV3idInUsersTable < ActiveRecord::Migration

  def up
    User.all.each do |u|
      old_u = OldUser.find_by( email: u.email )

      u.confirmed_at = Time.now    
      u.old_id = old_u.id unless old_u.nil?
      u.save
    end
  end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM