Like the question says, I am trying to import a CSV with the following function:
def import_users
path = Rails.root.join('somePath')
CSV.foreach(path, headers: true) do |row|
usr = User.new(row.to_hash)
usr.skip_callbacks = true
usr.save
end
puts "inserts on table users complete"
end
As you can tell, it is a simple function.
Issue:
All the columns with the correct data are imported correctly but in the User Class the password= function (setter) is triggered:
class User < ApplicationRecord
attr_accessor :skip_callbacks
validates :password, presence: true, unless: :skip_callbacks
def password= password
self[:password] = hash_password password
end
private
def hash_password password
Digest::MD5.hexdigest "#{Digest::MD5.hexdigest password}#{salt}"
end
end
This causes the password column in the database to be rewritten again with new hashes as passwords which then prevents login because the column values have changed. The password column in the database does not contain the same info as in the CSV File.
Normally I implemented a skip_callback to bypass password validation but does not seem to work.
I would really appreciate any kind of help as I have been trying to solve this for a couple of days now and can't seem to figure out why.