Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda