Search code examples
mysqlrubysqliteimportfastercsv

FasterCSV tutorial to import data to database?


Is anyone aware of any tutorials that demonstrate how to import data in a Ruby app with FasterCSV and saving it to a SQLite or MySQL database?

Here are the specific steps involved:

  1. Reading a file line by line (the .foreach method does this according to documentation)
  2. Mapping header names in file to database column names
  3. Creating entries in database for CSV data (seems doable with .new and .save within a .foreach block)

This is a basic usage scenario but I haven't been able to find any tutorials for it, so any resources would be helpful.

Thanks!


Solution

  • So it looks like FasterCSV is now part of the Ruby core as of Ruby 1.9, so this is what I ended up doing, to achieve the goals in my question above:

    @importedfile = Import.find(params[:id])
    filename = @importedfile.csv.path
    CSV.foreach(filename, {:headers => true}) do |row|
      @post = Post.find_or_create_by_email(
        :content          =>  row[0],
        :name             =>  row[1],
        :blog_url         =>  row[2],
        :email            =>  row[3]
      )
    end
    flash[:notice] = "New posts were successfully processed."
    redirect_to posts_path
    

    Inside the find_or_create_by_email function is the mapping from the database columns to the columns of the CSV file: row[0], row[1], row[2], row[3].

    Since it is a find_or_create function I don't need to explicitly call @post.save to save the entry to the database.

    If there's a better way please update or add your own answer.