By Jason


2013-02-28 14:05:25 8 Comments

I'm trying to add rows of a CSV to records in a table and everything seems to be working fine besides the fact that nothing actually shows up.

Here is my Rake task:

namespace :csv do

  desc "Import CSV Data"
  task :c_up => :environment do

    require 'csv'

    csv_file_path = 'db/main.csv'

    CSV.foreach(csv_file_path, :headers => true) do |row|
      row = Entry.create!({
        :one => row[0],
        :two => row[1],
        :three => row[2],
        :four => row[3],
        :five =>row[4],
        :six => row[5],
        :seven => row[6],
        :eight => row[7],
        :nine => row[8],
        :ten => row[9]
      })
      row.save
      puts "Entry added!"

    end
  end
end

The headers in my CSV are named to match the attributes.

When I run the task I receive no errors and all of the "Entry added!" show up in the console. I've tried it without the row = and row.save as I've seen lots of examples without those but it doesn't help.

After the rake finishes there aren't any records at all. Is there something obvious I'm doing wrong? It seems like this is following most examples I've seen.

Edit Thanks for all your answers and comments! Obviously this is flawed all the way down to syntax, I'm going to try out your suggestions and I'll update this after. Thanks again!

Edit 2 It turned out to be a syntax issue that you guys all pointed out, so +1 for everyone thanks! The reason when I commented it still wasn't working was an unrelated problem which I wouldn't have figured out without @jvnill's comments.

3 comments

@Muhamamd Awais 2013-02-28 14:52:42

namespace :csv do

desc "Import CSV Data" task :c_up => :environment do

require 'csv'

csv_file_path = 'db/main.csv'

CSV.foreach(csv_file_path, :headers => true) do |row|
  Entry.create!({
    :one => row[0],
    :two => row[1],
    :three => row[2],
    :four => row[3],
    :five =>row[4],
    :six => row[5],
    :seven => row[6],
    :eight => row[7],
    :nine => row[8],
    :ten => row[9]
  })
end

end end

@Aleks 2013-02-28 14:15:38

try with:

row = Entry.new(
        :one => row[0],
        :two => row[1],
        :three => row[2],
        :four => row[3],
        :five =>row[4],
        :six => row[5],
        :seven => row[6],
        :eight => row[7],
        :nine => row[8],
        :ten => row[9]
      )
      row.save!

Without { } , and with save!

You could even try with this, to make it simpler:

Entry.create!(row.to_hash)

@Richard Brown 2013-02-28 14:10:17

Entry.create! does not require being assigned to row nor does it require a row.save. Also know '{}' Just do:

Entry.create!(
        :one => row[0],
        :two => row[1],
        :three => row[2],
        :four => row[3],
        :five =>row[4],
        :six => row[5],
        :seven => row[6],
        :eight => row[7],
        :nine => row[8],
        :ten => row[9]
      )

The create method will populate the row and save it as well.

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] How do I import CSV file into a MySQL table?

27 Answered Questions

[SOLVED] Dealing with commas in a CSV file

  • 2009-04-20 18:52:33
  • Bob The Janitor
  • 526062 View
  • 472 Score
  • 27 Answer
  • Tags:   csv

18 Answered Questions

[SOLVED] Save PL/pgSQL output from PostgreSQL to a CSV file

36 Answered Questions

[SOLVED] How to output MySQL query results in CSV format?

  • 2008-12-10 15:59:51
  • MCS
  • 1217635 View
  • 1191 Score
  • 36 Answer
  • Tags:   mysql csv quotes

26 Answered Questions

6 Answered Questions

[SOLVED] How to update attributes without validation

36 Answered Questions

[SOLVED] Excel to CSV with UTF8 encoding

19 Answered Questions

[SOLVED] Purge or recreate a Ruby on Rails database

2 Answered Questions

[SOLVED] Import CSV to existing sqlite database table

Sponsored Content