Import whole CSV files from specific folder
In this example, lets say we have many product CSV files in a folder. Each CSV file need to upload our database from our console write a command. Run the following command in a new or existing project to create this model.
Uploads CSV from console command
Section titled “Uploads CSV from console command”Terminal Commands:
rails g model Product name:string quantity:integer price:decimal{12,2}rake db:migrateLet’s create a controller.
Terminal Commands:
rails g controller ProductsController Code:
class HistoriesController < ApplicationController def create file = Dir.glob("#{Rails.root}/public/products/**/*.csv") #=> This folder directory where read the CSV files file.each do |file| Product.import(file) end endendModel:
class Product< ApplicationRecord def self.import(file) CSV.foreach(file.path, headers: true) do |row| Product.create! row.to_hash end endendroutes.rb
resources :productsapp/config/application.rb
require 'csv'Now open your development console & run
=> ProductsController.new.create #=> Uploads your whole CSV files from your folder directory