LoginSignup
0
1

More than 1 year has passed since last update.

active_hashを使って都道府県モデルを作る方法

Posted at

はじめに

 
 今回は都道府県を選択する時に使う「active_hash」について新たに学んだので備忘録としてまとめます。active_hash住所などを選択する時に都道府県を選択するプルダウンを作るために必要です。ユーザー登録をする時にはよく使いますが、47個の都道府県を毎回実装するのは大変です。そんな時に、active_hashを使うと簡単に効率よく実装する事ができます。

active_hashの実装方法

active_hashの実装方法は以下の手順です。

1gemfile
2疑似モデルを作る
3対応するカラムを作る
4アソシエーションを定義する
です。
今回はuserモデルに都道府県を保持させたい場合を想定します

それでは順番に説明していきます。

gemfileの導入

 まずはgemfileを導入します。

 ## Gemfileに以下を記述
 gem 'active_hash'

その後bundle installを忘れずします。

都道府県の擬似モデルを作る

 次に都道府県を入れた擬似モデルを作ります
まずは実施のコードをご覧ください

prefecture.rb
class Prefecture < ActiveHash::Base
  self.data = [
    { 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: '鹿児島県' }, { id: 48, name: '沖縄県' }
  ]

end

このようにして都道府県の擬似モデルを作ります。
ここで注意してもらいたいのが、
「class Prefecture < ActiveHash::Base」の部分です。
ActiveHash::Baseという記述は、あるモデルの中でActiveHashを使うために必要となるクラスです。
ActiveHash::Baseを継承することで、ActiveRecordと同じようなメソッドを使用できるようになります。
従って今回の例で言うと、user.prefecture.nameであるuserの出身地をとってくる事ができます。

適用したいモデルに、prefecture_idカラムを作る

 次に都道府県を使用したいモデルにprefecture_idカラムを追加します。
今回はuserのマイグレーションファイルに記述します。

t.integer :prefecture_id,    null: false

注意点としては、
・id値が数値なのでintegerを指定する事
・カラム名に_idをつける事です
このようにする事で、userに紐づく都道府県をとってくる事ができます。

アソシエーションの設定

 次にuserモデルとprefectureモデル間でのアソシエーションを設定します
まずはuserモデルです。

user.rb
class User < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :prefecture
end

userは必ず一つの都道府県に紐づいているので、prefectureに対してbelongs_toの関係です。
ここで注意点はextend ActiveHash::Associations::ActiveRecordExtensionsと記述してモジュールを取り込んでいる事です。

次にprefectureモデルです。

prefecture.rb
class Prefecture < ActiveHash::Base
  self.data = [
    { 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: '茨城県' },
〜〜中略〜〜
  ]

  include ActiveHash::Associations
  has_many :items
end

都道府県は複数のuserに紐づくので、userに対してhas_manyの関係です。
注意点としてはinclude ActiveHash::Associationsと記述してモジュールを取り込んでいる事です。
 
以上ここまでで都道府県モデルを作る事ができした。

まとめ

 今回はactive_hashを使って都道府県モデルを作る方法についてまとめました。
 active_hashを使う利点としては固定値の使い回しができる事です。都道府県はuser以外にもいろいろな場面で使われます。そのような時に都道府県モデルがあると、効率的に実装を進める事ができます。

以上今回の記事が少しでも誰かのお役に立てれば幸いです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1