문제

In my app, I added a new row to User table, using migration, as a foreign key for users table:

class AddProfileIdToUsersTable < ActiveRecord::Migration
  def change
    add_column :users, :profile_id, :integer
  end
end

And then populate Profiles table with some Users data, before updating the profile_id of a user by the newly created profile.id:

class MigrateSomeUsersInfoToProfilesTable < ActiveRecord::Migration
  def up
    User.all.each do |u|
      profile = Profile.new({
        first_name: u.first_name,
        last_name: u.last_name,
        bio: u.bio,
        address: u.address,
      })
      profile.save!

      u.profile_id = profile.id
      u.save!
    end
  end
end

This successfully inserts values in profiles table and update profile_id in users table, but it also sends a email to each user! Here is the log:

2014-06-04 18:43:40.747 [INFO ] Rendered devise/mailer/confirmation_instructions.html.haml (21.5ms) (pid:2)
2014-06-04 18:43:41.725 [INFO ] Sent mail to xxxxxx@gmail.com (974.2ms) (pid:2)
2014-06-04 18:43:43.425 [INFO ] Rendered devise/mailer/confirmation_instructions.html.haml (1.1ms) (pid:2)
2014-06-04 18:43:44.197 [INFO ] Sent mail to yyyyyy@yahoo.com (769.8ms) (pid:2)
....

Which is quite annoying! Any idea how to avoid that?

Thanks

도움이 되었습니까?

해결책

You can call skip_confirmation before saving the user

class MigrateSomeUsersInfoToProfilesTable < ActiveRecord::Migration
  def up
    User.all.each do |u|
      profile = Profile.new({
        first_name: u.first_name,
        last_name: u.last_name,
        bio: u.bio,
        address: u.address,
      })
      profile.save!

      u.profile_id = profile.id
      u.skip_confirmation!
      u.save!
    end
  end
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top