Ask A Question

Notifications

You’re not receiving notifications from this thread.

Introduction to Importing from CSV Discussion

Actually - I tried the steps I described below with just a few lines, that I amended manually to insert a pipe between cell values.

When i try to rake the task I get an error that says:

bundle exec rake import:randd_fields --trace

** Invoke import:randd_fields (first_time)

** Invoke environment (first_time)

** Execute environment

** Execute import:randd_fields

nil

rake aborted!

NameError: undefined local variable or method `title' for main:Object

Has anyone been able to get this tutorial to work?

Reply

my client thought it was a simple feature, but I told him from the beginning it was not, now you confirm the fact. Thank you.

Reply

I am really stuck in trying to follow along with this. I moved my xls data into a google drive sheet and downloaded that as a csv saved in my root directory, but it still isn't working. Are there any other references to different tutorials that i can try. I tried those published by RichonRails and Matt Morgante but those don't work either. I tried the roo gem with the 2012 rails cast but that is out of date and I cant get that configured to work. Please can you go deeper into this so that the issue I keep running into is covered in your process. Thank you

Reply

Hello Chris.
I tried to run this code but i am getting this error, can you please help me.

https://uploads.disquscdn.c... https://uploads.disquscdn.c...

Reply
So @Chris how to you write Test for this solution?
Reply

Hey Chris -

You mentioned that User.create without the ! will allow an attempt to save to the db to fail silently if the record already exists, but isn't that only true if you've created a uniqueness constraint on the User model? That's the only way I could make it work. Using postgres.

Reply

Correct, you need validations for uniqueness so you don't get duplicates. Email is typically the field marked for uniqueness.

Reply

Hello Chris
Would you like to show me how we read the name of every sheet and all the object from one sheet . Because i am stuck hear. I always getting error like this
NoMethodError (undefined method `row' for "sheet_name":String):

Reply

looking for the furthur improvements.

Reply

I am also looking for the futhur information.
Thanks
Liteblue

Reply

Late to the party and all that but if anybody is looking to seed their project database from a CSV file, the is what I enter in my seed.rb file(s):

'''
CSV.read(Rails.root.join('lib', 'seeds', 'file_with_data_to_import.csv'), headers: true).each do |row|
t = ImportingModelName.create(row.to_hash)
puts "#{t.name}, #{t.some_other_attribute} saved"
end

// Optional part
puts "There are now #{ImportingModelName.count} rows in your ImportingModelName database table."
'''

The same can be used in a Rake Task file as per Chris’s video. It assumes that the CSV file has header row but, for a scheduled task, you could take care of those on the fly as per the video.

Performance-wise, it works rather well. For example, I had to seed a project database with a bit fewer than 25,000 records, which took about 2 minutes on my development machine. Each record had 40 columns, 20 of which had to be validated, and I had to modify the text case of the content in 6 of the columns.

Hope this helps someone.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.