Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
Trying to import a CSV file triggers a function which ends up manipulating the data imported into the database

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda