0

i am trying to import all csv file data to my rails db but one attributes Area value getting nil data

my csv file data enter image description here

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. enter image description here

2
  • 1
    Does this answer your question? CSV.foreach Not Reading First Column in CSV File Commented Mar 23, 2022 at 16:54
  • 1
    Please replace the picture of your CSV file with text, thereby allowing readers to cut-and-paste your CSV data to test their code. Commented Mar 23, 2022 at 17:41

2 Answers 2

0

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]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.