How to input multiple images at once using rake task

Introduction

From active_admin (administrator screen) using Carrierwave I was posting images one by one. You need to delete the data in the database once Therefore,

Ke: "Is it going to be posted one by one again? I'm tired, is there any good way?"

It started from. Lol

Contents

The file changed this time is as follows

lib/tasks/import_csv.rake db/csv_data/text_data.csv

It was a lot of work. Lol

Let's go

solution

Originally here

lib/tasks/import_csv.rake


desc "Task to import TEXT data"
    task movie_data: :environment do
        list = Import.csv_data(path: "db/csv_data/text_data.csv")
        Text.create!(list)
    end

The image cannot be uploaded at this rate. Then use the open method to instruct it to open the image file.

lib/tasks/import_csv.rake


desc "Task to import TEXT data"
    task text: :environment do
        list = Import.csv_data(path: "db/csv_data/text_data.csv")
               * File.Rails with join.root+"db/csv_data/text_data.csv"To combine.
        path = File.join Rails.root, "db/csv_data/text_data.csv"
        CSV.foreach(path, headers: true) do |row|
            list << {         
                image: File.open("#{Rails.root}/#{row['image']}")
            }
          end
          puts "Start import process"
        Text.create!(list)
          puts "Import was successful"
    end

Since CSV.foreach is used to process lines one by one, it can be used without worrying about memory usage even when handling CSV files with many lines.

After finishing this description, the next step is to edit the CSV file!

db/csv_data/text_data.csv


image,genre,title,content
"db/fixtures/Slide 2.jpeg ",Basic,Mac shortcut keys,"If you are not familiar with your computer, let's start by using shortcut keys. If you can't use the shortcut keys listed here, the learning efficiency of programming will not improve at all.

Describe db/fixtures/slide 2.jpeg and the path of the file. In my case, I still have more data, so Give the path of the file in the same way ...

** This was sober ** ww

After writing, let's import the task next!

Terminal



rake -T 
.
.
.
.
rake import_csv:text                    #Task to import TEXT data

Check the command Copy and execute rake import_csv: text

Terminal


rake import_csv:text
.
.
.
.
Start import process
rake aborted!
ActiveRecord::RecordInvalid:Validation failed:Title cannot be left blank,Genre cannot be blank,Content cannot be blank

I thought the code was a little strange, but it seems like it's still lol

When I read the error statement, it seems that there is no data in other columns,

Let's look at the code again.

lib/tasks/import_csv.rake


desc "Task to import TEXT data"
    task text: :environment do
        list = Import.csv_data(path: "db/csv_data/text_data.csv")
               * File.Rails with join.root+"db/csv_data/text_data.csv"To combine.
        path = File.join Rails.root, "db/csv_data/text_data.csv"
        CSV.foreach(path, headers: true) do |row|
            list << {         
                image: File.open("#{Rails.root}/#{row['image']}")
       !!?? *I only put image in the list lol
            }
          end
          puts "Start import process"
        Text.create!(list)
          puts "Import was successful"
    end

Added below

lib/tasks/import_csv.rake


desc "Task to import TEXT data"
    task text: :environment do
        list = Import.csv_data(path: "db/csv_data/text_data.csv")
        path = File.join Rails.root, "db/csv_data/text_data.csv"
        CSV.foreach(path, headers: true) do |row|
            list << {
                image: File.open("#{Rails.root}/#{row['image']}"),
               #Added below
                genre: row["genre"],
                title: row["title"],
                content: row["content"]
            }
          end
          puts "Start import process"
        Text.create!(list)
          puts "Import was successful"
    end

Import again rake import_csv:text

Start import process
Import was successful

You succeeded! Check if there is any content just in case.

console


pry(main)> Text.select("image")
.
.
.
.
#<Text:0x00007fb4e4c29f98 id: nil, image: "Slide 2.jpeg ">,

It's firmly in!

It's a rough content, but that's it.

It's nice to write it as an article because the information can be fixed.

Referenced articles

https://remonote.jp/rails-carrierwave-seed

Recommended Posts

How to input multiple images at once using rake task
[Rails] How to upload multiple images using Carrierwave
[Rails 6] Two methods to import multiple images at once using CarrierWave / (1) Input with initial seed data / (2) Import with CSV
How to call multiple names at once in the same category
[Rails] How to upload images using Carrierwave
Rails: How to write a rake task nicely
How to save to multiple tables with one input
How to insert all at once with MyBatis
How to link images using FactoryBot Active Storage
How to test including images when using ActiveStorage and Faker
[rails] How to post images
How to handle uploaded images
How to minimize Java images
[Rails] How to delete images uploaded by carrierwave (using devise)
How to authorize using graphql-ruby
I implemented the code to learn multiple images at once in the Watson Visual Recognition Collection in Java.
[Rails 6] How to create a dynamic form input screen using cocoon
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
[rails6.0.0] How to save images using Active Storage in wizard format
[Swift] How to replace multiple strings
[Rails] Status update using Rake task
How to build CloudStack using Docker
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Android] How to pass images and receive callbacks when sharing using ShareCompat
How to execute a contract using web3j
How to sort a List using Comparator
How to switch thumbnail images with JavaScript
[Java] How to calculate age using LocalDate
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
How to make asynchronous pagenations using Kaminari
How to switch between multiple Java versions
[Rails] How to handle data using enum
Consume multiple messages at once with spring-kafka
How to execute multiple commands in docker-compose.yml
How to insert icons using Font awesome
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once