I am importing a CSV file using 'csv'. Import is working as expected, but I would like to update existing records, based on a secondary key field.
I am using the following code:
CSV.foreach(path, :headers => true) do |row|
if(Product.exists?(secondary_key: row['secondary_key']))
#Update goes here
else
Product.create!(row.to_hash)
end
I have tried (among others):
product = Product.where(:secondary_key => row['secondary_key'])
Product.update(product, row.to_hash)
Now that trial-and-error is not bringing me anywhere, I would appreciate your help!