Home > Blockchain >  Trying to import a CSV file triggers a function which ends up manipulating the data imported into th
Trying to import a CSV file triggers a function which ends up manipulating the data imported into th

Time:12-22

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.

CodePudding user response:

Looks like you want to add passwords to database as is without hashing, and validations have nothing to do with it because they will not change the behavior of the password= function. You may patch it with another instance variable like:

class User < ApplicationRecord

  attr_accessor :skip_callbacks
  attr_accessor :skip_password_hashing
  validates :password, presence: true, unless: :skip_callbacks

  def password= password
    self[:password] = skip_password_hashing ? password : hash_password(password)
  end

  private
    
  def hash_password password
     Digest::MD5.hexdigest "#{Digest::MD5.hexdigest password}#{salt}"
  end
end

and then patch import like:

def import_users
  path = Rails.root.join('somePath')
  CSV.foreach(path, headers: true) do |row|
    usr = User.new(row.to_hash)
    usr.skip_password_hashing = true
    usr.save
  end
  puts "inserts on table users complete"
end

I am not sure if it is a good idea to have this possibility in deployed application, but it should work.

  • Related