Devise: manually encrypt password and store directly

前端 未结 5 997
[愿得一人]
[愿得一人] 2021-02-01 02:27

I\'m trying to migrate a ton of users from an old database. To do this, I\'m using activerecord-import and trying to save all my user data directly to DB (bypassing the User mo

5条回答
  •  不要未来只要你来
    2021-02-01 03:12

    Assuming you have a mysql database with a "users" table and a "password" column And an ActiveRecord model class called "user" that is hooked up to devise

    Create an ActiveRecord model class in your app app/models/old_user.rb

    OldUser < ActiveRecord::Base
      set_table :users
      establish_connection :database => "old_database", :user => "old user", :adapter => "mysql"
    end
    

    then create a rake task: app/lib/tasks/migrate_users.rake

    task :migrate_users => :environment do
      OldUser.find_each do |old_user|
        u = User.new(:email => old_user.email, :password => old_user.password, :password_confirmation => old_user.password);
        #if your using confirmation
        u.skip_confirmation!
        u.save!
      end
    end
    

    Modify as necessary (make sure you're saving any app-specific user attributes)

    Then$ rake migrate_users

提交回复
热议问题