RailsのGem seed-fuを利用して都道府県の初期データを作成する

都道府県番号(厚生労働省)

#### Prefecture Tableの準備

[admin@postgresql weather]$ bundle exec rails g model prefecture
      invoke  active_record
      create    db/migrate/20200612225551_create_prefectures.rb
      create    app/models/prefecture.rb
      invoke    test_unit
      create      test/models/prefecture_test.rb
      create      test/fixtures/prefectures.yml
[admin@postgresql weather]$ 

[admin@postgresql weather]$ vim db/migrate/20200612225551_create_prefectures.rb
[admin@postgresql weather]$ cat db/migrate/20200612225551_create_prefectures.rb
class CreatePrefectures < ActiveRecord::Migration[6.0]
  def change
    create_table :prefectures do |t|
      t.string :name, null: false,  comment: "都道府県"
      t.timestamps
    end
  end
end
[admin@postgresql weather]$ 


[admin@postgresql weather]$ bundle exec rails db:migrate
== 20200612225551 CreatePrefectures: migrating ================================
-- create_table(:prefectures)
   -> 0.1382s
== 20200612225551 CreatePrefectures: migrated (0.1387s) =======================


#### Prefecture Tableの確認


weather_development-# \d prefectures;
                                           テーブル"public.prefectures"
     列     |               型               | 照合順序 | Null 値を許容 |               デフォルト                
------------+--------------------------------+----------+---------------+-----------------------------------------
 id         | bigint                         |          | not null      | nextval('prefectures_id_seq'::regclass)
 name       | character varying              |          | not null      | 
 created_at | timestamp(6) without time zone |          | not null      | 
 updated_at | timestamp(6) without time zone |          | not null      | 
インデックス:
    "prefectures_pkey" PRIMARY KEY, btree (id)
参照元:
    TABLE "weather_forecasts" CONSTRAINT "fk_rails_514be57da6" FOREIGN KEY (prefecture_id) REFERENCES prefectures(id)


#### Gem(seed-fu)のインストールとデータの準備

[admin@postgresql weather]$ tail Gemfile
# https://github.com/mbleigh/seed-fu
  gem 'seed-fu'

[admin@postgresql weather]$ bundle install
Fetching seed-fu 2.3.9
Installing seed-fu 2.3.9
<SNIP>

[admin@postgresql weather]$ cat db/fixtures/prefecture.rb 
Prefecture.seed(:id,
  { :id => 1, :name => "北海道" },
  { :id => 2, :name => "青森県" },
  { :id => 3, :name => "岩手県" },
  { :id => 4, :name => "宮城県" },
  { :id => 5, :name => "秋田県" },
  { :id => 6, :name => "山形県" },
  { :id => 7, :name => "福島県" },
  { :id => 8, :name => "茨城県" },
  { :id => 9, :name => "栃木県" },
  { :id => 10, :name => "群馬県" },
  { :id => 11, :name => "埼玉県" },
  { :id => 12, :name => "千葉県" },
  { :id => 13, :name => "東京都" },
  { :id => 14, :name => "神奈川県" },
  { :id => 15, :name => "新潟県" },
  { :id => 16, :name => "富山県" },
  { :id => 17, :name => "石川県" },
  { :id => 18, :name => "福井県" },
  { :id => 19, :name => "山梨県" },
  { :id => 20, :name => "長野県" },
  { :id => 21, :name => "岐阜県" },
  { :id => 22, :name => "静岡県" },
  { :id => 23, :name => "愛知県" },
  { :id => 24, :name => "三重県" },
  { :id => 25, :name => "滋賀県" },
  { :id => 26, :name => "京都府" },
  { :id => 27, :name => "大阪府" },
  { :id => 28, :name => "兵庫県" },
  { :id => 29, :name => "奈良県" },
  { :id => 30, :name => "和歌山県" },
  { :id => 31, :name => "鳥取県" },
  { :id => 32, :name => "島根県" },
  { :id => 33, :name => "岡山県" },
  { :id => 34, :name => "広島県" },
  { :id => 35, :name => "山口県" },
  { :id => 36, :name => "徳島県" },
  { :id => 37, :name => "香川県" },
  { :id => 38, :name => "愛媛県" },
  { :id => 39, :name => "高知県" },
  { :id => 40, :name => "福岡県" },
  { :id => 41, :name => "佐賀県" },
  { :id => 42, :name => "長崎県" },
  { :id => 43, :name => "熊本県" },
  { :id => 44, :name => "大分県" },
  { :id => 45, :name => "宮崎県" },
  { :id => 46, :name => "鹿児島県" },
  { :id => 47, :name => "沖縄県" }
)
[admin@postgresql weather]$ 

#### Gem(seed-fu)にてデータの投入


[admin@postgresql weather]$ bundle exec rails db:seed_fu

== Seed from /home/admin/app/ruby/weather/db/fixtures/prefecture.rb
 - Prefecture {:id=>1, :name=>"北海道"}
 - Prefecture {:id=>2, :name=>"青森県"}
 - Prefecture {:id=>3, :name=>"岩手県"}
 - Prefecture {:id=>4, :name=>"宮城県"}
 - Prefecture {:id=>5, :name=>"秋田県"}
 - Prefecture {:id=>6, :name=>"山形県"}
 - Prefecture {:id=>7, :name=>"福島県"}
 - Prefecture {:id=>8, :name=>"茨城県"}
 - Prefecture {:id=>9, :name=>"栃木県"}
 - Prefecture {:id=>10, :name=>"群馬県"}
 - Prefecture {:id=>11, :name=>"埼玉県"}
 - Prefecture {:id=>12, :name=>"千葉県"}
 - Prefecture {:id=>13, :name=>"東京都"}
 - Prefecture {:id=>14, :name=>"神奈川県"}
 - Prefecture {:id=>15, :name=>"新潟県"}
 - Prefecture {:id=>16, :name=>"富山県"}
 - Prefecture {:id=>17, :name=>"石川県"}
 - Prefecture {:id=>18, :name=>"福井県"}
 - Prefecture {:id=>19, :name=>"山梨県"}
 - Prefecture {:id=>20, :name=>"長野県"}
 - Prefecture {:id=>21, :name=>"岐阜県"}
 - Prefecture {:id=>22, :name=>"静岡県"}
 - Prefecture {:id=>23, :name=>"愛知県"}
 - Prefecture {:id=>24, :name=>"三重県"}
 - Prefecture {:id=>25, :name=>"滋賀県"}
 - Prefecture {:id=>26, :name=>"京都府"}
 - Prefecture {:id=>27, :name=>"大阪府"}
 - Prefecture {:id=>28, :name=>"兵庫県"}
 - Prefecture {:id=>29, :name=>"奈良県"}
 - Prefecture {:id=>30, :name=>"和歌山県"}
 - Prefecture {:id=>31, :name=>"鳥取県"}
 - Prefecture {:id=>32, :name=>"島根県"}
 - Prefecture {:id=>33, :name=>"岡山県"}
 - Prefecture {:id=>34, :name=>"広島県"}
 - Prefecture {:id=>35, :name=>"山口県"}
 - Prefecture {:id=>36, :name=>"徳島県"}
 - Prefecture {:id=>37, :name=>"香川県"}
 - Prefecture {:id=>38, :name=>"愛媛県"}
 - Prefecture {:id=>39, :name=>"高知県"}
 - Prefecture {:id=>40, :name=>"福岡県"}
 - Prefecture {:id=>41, :name=>"佐賀県"}
 - Prefecture {:id=>42, :name=>"長崎県"}
 - Prefecture {:id=>43, :name=>"熊本県"}
 - Prefecture {:id=>44, :name=>"大分県"}
 - Prefecture {:id=>45, :name=>"宮崎県"}
 - Prefecture {:id=>46, :name=>"鹿児島県"}
 - Prefecture {:id=>47, :name=>"沖縄県"}
[admin@postgresql weather]$ 

#### データの確認
都道府県データが正常に作成されている事を確認しました。

weather_development=# select * from prefectures;
 id |   name   |         created_at         |         updated_at         
----+----------+----------------------------+----------------------------
  1 | 北海道   | 2020-06-12 23:54:33.283651 | 2020-06-12 23:54:33.283651
  2 | 青森県   | 2020-06-12 23:54:33.294884 | 2020-06-12 23:54:33.294884
  3 | 岩手県   | 2020-06-12 23:54:33.301876 | 2020-06-12 23:54:33.301876
  4 | 宮城県   | 2020-06-12 23:54:33.309521 | 2020-06-12 23:54:33.309521
  5 | 秋田県   | 2020-06-12 23:54:33.315667 | 2020-06-12 23:54:33.315667
  6 | 山形県   | 2020-06-12 23:54:33.322739 | 2020-06-12 23:54:33.322739
  7 | 福島県   | 2020-06-12 23:54:33.330228 | 2020-06-12 23:54:33.330228
  8 | 茨城県   | 2020-06-12 23:54:33.352692 | 2020-06-12 23:54:33.352692
  9 | 栃木県   | 2020-06-12 23:54:33.361259 | 2020-06-12 23:54:33.361259
 10 | 群馬県   | 2020-06-12 23:54:33.368557 | 2020-06-12 23:54:33.368557
 11 | 埼玉県   | 2020-06-12 23:54:33.377314 | 2020-06-12 23:54:33.377314
 12 | 千葉県   | 2020-06-12 23:54:33.385298 | 2020-06-12 23:54:33.385298
 13 | 東京都   | 2020-06-12 23:54:33.394371 | 2020-06-12 23:54:33.394371
 14 | 神奈川県 | 2020-06-12 23:54:33.40101  | 2020-06-12 23:54:33.40101
 15 | 新潟県   | 2020-06-12 23:54:33.409158 | 2020-06-12 23:54:33.409158
 16 | 富山県   | 2020-06-12 23:54:33.416567 | 2020-06-12 23:54:33.416567
 17 | 石川県   | 2020-06-12 23:54:33.423705 | 2020-06-12 23:54:33.423705
 18 | 福井県   | 2020-06-12 23:54:33.434071 | 2020-06-12 23:54:33.434071
 19 | 山梨県   | 2020-06-12 23:54:33.464188 | 2020-06-12 23:54:33.464188
 20 | 長野県   | 2020-06-12 23:54:33.473083 | 2020-06-12 23:54:33.473083
 21 | 岐阜県   | 2020-06-12 23:54:33.483633 | 2020-06-12 23:54:33.483633
 22 | 静岡県   | 2020-06-12 23:54:33.4898   | 2020-06-12 23:54:33.4898
 23 | 愛知県   | 2020-06-12 23:54:33.507801 | 2020-06-12 23:54:33.507801
 24 | 三重県   | 2020-06-12 23:54:33.516001 | 2020-06-12 23:54:33.516001
 25 | 滋賀県   | 2020-06-12 23:54:33.523702 | 2020-06-12 23:54:33.523702
 26 | 京都府   | 2020-06-12 23:54:33.533514 | 2020-06-12 23:54:33.533514
 27 | 大阪府   | 2020-06-12 23:54:33.541808 | 2020-06-12 23:54:33.541808
 28 | 兵庫県   | 2020-06-12 23:54:33.550734 | 2020-06-12 23:54:33.550734
 29 | 奈良県   | 2020-06-12 23:54:33.557281 | 2020-06-12 23:54:33.557281
 30 | 和歌山県 | 2020-06-12 23:54:33.566571 | 2020-06-12 23:54:33.566571
 31 | 鳥取県   | 2020-06-12 23:54:33.573718 | 2020-06-12 23:54:33.573718
 32 | 島根県   | 2020-06-12 23:54:33.582212 | 2020-06-12 23:54:33.582212
 33 | 岡山県   | 2020-06-12 23:54:33.589471 | 2020-06-12 23:54:33.589471
 34 | 広島県   | 2020-06-12 23:54:33.598941 | 2020-06-12 23:54:33.598941
 35 | 山口県   | 2020-06-12 23:54:33.605716 | 2020-06-12 23:54:33.605716
 36 | 徳島県   | 2020-06-12 23:54:33.614624 | 2020-06-12 23:54:33.614624
 37 | 香川県   | 2020-06-12 23:54:33.621642 | 2020-06-12 23:54:33.621642
 38 | 愛媛県   | 2020-06-12 23:54:33.628824 | 2020-06-12 23:54:33.628824
 39 | 高知県   | 2020-06-12 23:54:33.637536 | 2020-06-12 23:54:33.637536
 40 | 福岡県   | 2020-06-12 23:54:33.644695 | 2020-06-12 23:54:33.644695
 41 | 佐賀県   | 2020-06-12 23:54:33.653393 | 2020-06-12 23:54:33.653393
 42 | 長崎県   | 2020-06-12 23:54:33.659903 | 2020-06-12 23:54:33.659903
 43 | 熊本県   | 2020-06-12 23:54:33.66802  | 2020-06-12 23:54:33.66802
 44 | 大分県   | 2020-06-12 23:54:33.675145 | 2020-06-12 23:54:33.675145
 45 | 宮崎県   | 2020-06-12 23:54:33.683799 | 2020-06-12 23:54:33.683799
 46 | 鹿児島県 | 2020-06-12 23:54:33.700625 | 2020-06-12 23:54:33.700625
 47 | 沖縄県   | 2020-06-12 23:54:33.708507 | 2020-06-12 23:54:33.708507
(47 行)

weather_development=#