[Rails] Implementation of CSV import function

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introductionIntroduction of Bootstrap3Implementation of posting function

Implementation

1. Introduce Gem

Gemfile


#Postscript
gem 'roo'

Terminal


$ bundle

2. Edit ʻapplication.rb`

application.rb


require_relative 'boot'

require 'rails/all'
require 'csv' #Postscript

Bundler.require(*Rails.groups)

module Bookers2Debug
  class Application < Rails::Application
    config.load_defaults 5.2
  end
end

3. Model editing

book.rb


def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    book = find_by(id: row["id"]) || new
    book.attributes = row.to_hash.slice(*updatable_attributes)
    book.save!(validate: false)
  end
end

def self.updatable_attributes
  ['id', 'title', 'body']
end

(1) If the same ID is found in the data to be imported, the record is called, and if not found, a new one is created.

book = find_by(id: row["id"]) || new

(2) Obtain data from the CSV file.

book.attributes = row.to_hash.slice(*updatable_attributes)

③ Save without passing through validation.

book.save!(validate: false)

(4) Set the columns to be received when importing CSV.

def self.updatable_attributes
  ['id', 'title', 'body']
end

4. Edit the controller

books_controller.rb


def import
  Book.import(params[:file])
  redirect_to books_path
end

5. Add routing

routes.rb


resources :books do
  collection { post :import }
end

6. Edit the view

slim:books/index.html.slim


= form_tag import_books_path, multipart: true do
  = file_field_tag :file
  br
  = submit_tag "import", class: 'btn btn-success'

Recommended Posts

[Rails] Implementation of CSV import function
[Rails] Implementation of CSV export function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
Rails [For beginners] Implementation of comment function
[Ruby on rails] Implementation of like function
Implementation of search function
Implementation of pagination function
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails] Implementation of image enlargement function using lightbox2
Rails implementation of ajax removal
Rails fuzzy search function implementation
Implementation of sequential search function
Implementation of like function (Ajax)
Implementation of image preview function
Implementation of category pull-down function
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of user logic deletion
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Ruby on Rails] CSV output function
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
Implementation of like function in Java
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Implementation of user authentication function using devise (2)
Rails Addition of easy and easy login function
[Ruby on Rails] Follow function implementation: Bidirectional
Implementation of user authentication function using devise (1)
Rails Basic CRUD function implementation procedure scaffold
Implementation of user authentication function using devise (3)
[Rails] Implementation of validation that maintains uniqueness
Rails CSV basics
DM function implementation
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Rails] Category function
Rails follow function
[Rails] Notification function
[Rails6 + Vue.js] Implement CSV import process using axios
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Rails] Implementation of "notify notification in some way"
Ruby on Rails Email automatic sending function implementation
Implementation of search function Learning memo (portfolio creation)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Implementation of delete function (if you have foreign_key)