gem active_hash About active hash

What I did last night Because the introduction of active hash was successful Memorandum

I mean I was impatient because I couldn't find the description of migration from the primary source ...

environment

ruby 2.5 rails 5.2.4.3 devise 4.7.2 active_hash 3.1.0

reference

zilkey/active_hash: collection_select

It is used to store a small amount of data.

I want to use several places in one app, such as data selection for prefectures, But not until I use DB. Hmm ... I recommend it at times. Can it be used in categories? ?? ↑ The category was a gem called ancestry. https://github.com/stefankroes/ancestry

Easy introduction

to gem

Gemfile


gem 'active_hash'

The usual

bundle install

model addition OTZ that was easy so far

☓ rails g model Model name ← No. This time it's prefecture data

models/prefecture.rb


class Prefecture < ActiveHash::Base
  self.data = [
      {id: 1, name: 'Hokkaido'}, {id: 2, name: 'Aomori Prefecture'}, {id: 3, name: 'Iwate Prefecture'},
      {id: 4, name: 'Miyagi Prefecture'}, {id: 5, name: 'Akita'}, {id: 6, name: 'Yamagata Prefecture'},
      {id: 7, name: 'Fukushima Prefecture'}, {id: 8, name: 'Ibaraki Prefecture'}, {id: 9, name: 'Tochigi Prefecture'},
      {id: 10, name: 'Gunma Prefecture'}, {id: 11, name: 'Saitama'}, {id: 12, name: 'Chiba'},
      {id: 13, name: 'Tokyo'}, {id: 14, name: 'Kanagawa Prefecture'}, {id: 15, name: 'Niigata Prefecture'},
      {id: 16, name: 'Toyama Prefecture'}, {id: 17, name: 'Ishikawa Prefecture'}, {id: 18, name: 'Fukui prefecture'},
      {id: 19, name: 'Yamanashi Prefecture'}, {id: 20, name: 'Nagano Prefecture'}, {id: 21, name: 'Gifu Prefecture'},
      {id: 22, name: 'Shizuoka Prefecture'}, {id: 23, name: 'Aichi prefecture'}, {id: 24, name: 'Mie Prefecture'},
      {id: 25, name: 'Shiga Prefecture'}, {id: 26, name: 'Kyoto'}, {id: 27, name: 'Osaka'},
      {id: 28, name: 'Hyogo prefecture'}, {id: 29, name: 'Nara Prefecture'}, {id: 30, name: 'Wakayama Prefecture'},
      {id: 31, name: 'Tottori prefecture'}, {id: 32, name: 'Shimane Prefecture'}, {id: 33, name: 'Okayama Prefecture'},
      {id: 34, name: 'Hiroshima Prefecture'}, {id: 35, name: 'Yamaguchi Prefecture'}, {id: 36, name: 'Tokushima Prefecture'},
      {id: 37, name: 'Kagawa Prefecture'}, {id: 38, name: 'Ehime Prefecture'}, {id: 39, name: 'Kochi Prefecture'},
      {id: 40, name: 'Fukuoka Prefecture'}, {id: 41, name: 'Saga Prefecture'}, {id: 42, name: 'Nagasaki Prefecture'},
      {id: 43, name: 'Kumamoto Prefecture'}, {id: 44, name: 'Oita Prefecture'}, {id: 45, name: 'Miyazaki prefecture'},
      {id: 46, name: 'Kagoshima prefecture'}, {id: 47, name: 'Okinawa Prefecture'}
  ]
end

model postscript As the basics of the primary source

This time address model association address many ← → 1 prefecture

models/address.rb


  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :prefecture

Change migration Information from here ...

Edit the corresponding migrationFile This time

migrate/*****_create_addresses.rb


t.integer :prefecture_id,        null: false, default: nil

null ~ is your choice ♪ After rewriting, the usual rails db: migrate Also rewrite strong params ^^ Maybe registrations_controller.rb Of: Only the prefecture part. I made it: prefecture_id.

registrations_controller.rb


  protected

  def address_params
    params.require(:address).permit(:family_name_kanji,:first_name_kanji,:family_name_kana,:first_name_kana,:post_number,:prefecture_id,:city,:block_number,:apartment_name,:phone_number)
  end

view change

The class was troublesome.

= f.collection_select :prefecture_id,Prefecture.all,:id,:name,{prompt:"--"},{class:"inputDefault"}

This is ↓.

<select class="inputDefault" name="address[prefecture_id]" id="address_prefecture_id"><option value="">--</option>
<option value="">--</option>
<option value="1">Hokkaido</option>
<option value="2">Aomori Prefecture</option>
<option value="3">Iwate Prefecture</option>
・ ・ ・ ・ ・ ・ ・
<option value="47">Fukuoka Prefecture</option></select>
= f.collection_select :Column name,Data to register,Data to display,First data(selected is also here),This time class

Maybe it's over. I didn't have the idea to change the migration column from t.string: prefecture to the above, so I tried hard to change the registration data of collection_select to: name ^^; A few hours TT in no time

Recommended Posts

gem active_hash About active hash
[Rails] About active hash
About active_hash
About Active Storage
[About double hash]
About Gem :: ConflictError (activesupport)
About DelegationError (Active Storage)
[Rails] Introducing Active Hash
Pitfalls of Active Hash
[Beginner] About Active Storage
Table design (using Active Hash)
About the behavior of ruby Hash # ==
I tried to explain Active Hash