Augusto
Augusto

Reputation: 1150

There's an easy way to load csv rows into an Heroku hosted app?

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

Answers (3)

Gaurav Saini
Gaurav Saini

Reputation: 137

Yes, there is a very simple way.

  1. Upload the file to gist or anywhere publically accessible.
  2. 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}

  3. You have all your required data.

    csv = CSV.new(csv_file) pry(main)> csv.first => ["Name", " Purpose"]

Upvotes: 0

user1147171
user1147171

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

Dylan Markow
Dylan Markow

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

Related Questions