开发者

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 i开发者_开发百科s already in the perfect format for the app that I'm hosting on Heroku. Is there an easy way to do it?

Thanks, Augusto


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


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


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"]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜