0

I am building a site with Ruby on Rails where I can display items that are for sale. Currently I can add new items by filling a form with title, price, some text and upload a couple of pictures.

Since there might be many items I would like to have a text file with the data, one item on each line, data separated with comma or whatever. Then I'd like to give this file to my Rail's application that will create the items in the application. Would this be possible?

  • http://stackoverflow.com/questions/4410794/ruby-on-rails-import-data-from-a-csv-file – sethi Jun 13 '16 at 13:42
  • http://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data will provide good information for your use case. – floum Jun 13 '16 at 13:50

1 Answers1

0

What you're basically talking about is importing and exporting CSV files.

You can learn about the basic ruby functions available to you when working with CSV files here: http://ruby-doc.org/stdlib-2.3.0/libdoc/csv/rdoc/CSV.html

Here's a video about exporting to csv and here's a video about importing from csv.

You essentially will call :to_csv on some collection, and then use CSV's foreach or some method of iterating the items in the CSV, and you will create your rows from the parsed data.

chad_
  • 3,749
  • 2
  • 22
  • 22
  • Thank you! Your comment was very helpful and I was able to implement the desired behavior. For future reference I also want to mention railscasts.com/episodes/396-importing-csv-and-excel as a good source to learn more about this even if it is a little bit dated. – Ylva Lindberg Jun 17 '16 at 20:12
  • You know, I had actually linked it and the related video about exporting, but thought maybe sharing an outdated video would confuse, more than help. I'm glad you were able to achieve your goal. I remember finding this CSV stuff to be particularly well implemented with Ruby, when I initially discovered it. I'm glad it works for your needs, too! – chad_ Jun 17 '16 at 20:33