Reputation: 1150
I have some csv exported rows of data that I need to load into a Rails 2.3.8 app.
The csv data is already in the perfect format for the app that I'm hosting on Heroku. Is there an easy way to do it?
Thanks, Augusto
Upvotes: 1
Views: 1210
Reputation: 137
Yes, there is a very simple way.
Use open-uri to load it in any console of your choice.
csv_file = open('https://gist.githubusercontent.com/gauravsaini23/84f9592bbc7cf444e869f8c67321a086/raw/84f7dc0c9d0770a30d28072e05527d1071870042/sample.csv') {|f| f.read}
You have all your required data.
csv = CSV.new(csv_file) pry(main)> csv.first => ["Name", " Purpose"]
Upvotes: 0
Reputation: 1243
I have had success with something like this:
require 'csv'
CSV.foreach(Rails.root.to_s+"/db/calseed_test1.csv") do |row|
Calevent.create(
:caldate => row[0],
:caltime => row[1],
:callocation => row[2],
:caldescription => row[3]
)
end
Upvotes: 4
Reputation: 124419
You could use Ruby's built-in CSV library to read your data into an array, which you can then do whatever you need to with:
CSV.foreach("path/to/file.csv") do |row|
# use row here...
# For example, if you had users in a CSV file like "username,email" you could do:
User.create(:username => row[0], :email => row[1])
end
Upvotes: 3