'Getting Nil value while importing csv file into rails database
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
Solution 1:[1]
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]
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | adarsh |
| Solution 2 | Prateek Choudhary |


