# frozen_string_literal: true

[
  ChannelGroup,
  Channel,
  NumberFormat,
  Prefecture,
  Tip,
  Work,
  Episode
].each do |model_class|
  file_path = "#{Dir.pwd}/db/data/csv/#{model_class.table_name}.csv"

  CSV.foreach(file_path, headers: true) do |row|
    record = model_class.where(id: row["id"]).first_or_create!(row.to_h.except("id"))
    puts "#{model_class}: #{record.id}"
  end
end

# frozen_string_literal: true

[
  ChannelGroup,
  Channel,
  NumberFormat,
  Prefecture,
  Tip,
  Work,
  Episode
].each do |model_class|
  file_path = "#{Dir.pwd}/db/data/csv/#{model_class.table_name}.csv"

  model_class.transaction do
    CSV.foreach(file_path, headers: true) do |row|
      record = model_class.where(id: row["id"]).first_or_create!(row.to_h.except("id"))
      puts "#{model_class}: #{record.id}"
    end
  end
end

Reduce the number of commits in db:seed for better performance
Reduce the number of commits in db:seed for better performance

Before

```
% bin/rails db:seed
% psql -c 'delete from episodes' annict_development
DELETE 1565
% time bin/rails db:seed
bin/rails db:seed  26.08s user 1.50s system 36% cpu 1:16.16 total
```

After

```
% bin/rails db:seed
% psql -c 'delete from episodes' annict_development
DELETE 1565
% time bin/rails db:seed
bin/rails db:seed  13.77s user 0.90s system 87% cpu 16.805 total
````
