i am trying to import all csv file data to my rails db but one attributes Area value getting nil data
IN my Seed file
require 'csv'
csv_text = File.read(Rails.root.join('db/product_data1.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') do |row|
Ada.create!( {
Area: row["Area"],
Item: row["Item"],
Year: row["Year"],
Value: row["Value"]
} )
end
in rails console i am geeting Area data is nil.
CodePudding user response:
I change the code it work for me.
Area: row["Area"],
Item: row["Item"],
Year: row["Year"],
Value: row["Value"]
into
Area: row[0],
Item: row[1],
Year: row[3],
Value: row[4]
CodePudding user response:
You need to make sure the file is saved with the correct encoding. I tried the same data with your example code and it worked for me.
You can verify it by copying over the same data to excel / google sheets, and save as .csv.
You could also check out this answer CSV.foreach Not Reading First Column in CSV File