[Ruby] Let’s create a pseudo model using active_hash ~prefecture data~

2 minute read

Creating a certain Mercari copy site #active_hash

Pseudo model can be created and data can be entered without creating a database

So I will try using a gem that is active_hash

First add active_hash to Gemfile


gem'active_hash'

do bundle install

Model

I am making a model file with This time it will not be created with rails g model

I usually inherit from ApplicationRecord, It seems that the model that uses active_hash needs to inherit ActiveHash::Base

So I will create it directly in app/models/

prefecture.rb


class Prefecture <ActiveHash::Base

end

Inherit ActiveHash::Base like this

The model of the data looks like this

prefecture.rb


class Prefecture <ActiveHash::Base

  self.data = []

end

I will put the data in this

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 prefecture'}, {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 prefecture'}, {id: 12, name:'Chiba prefecture'},
      {id: 13, name:'Tokyo'}, {id: 14, name:'Kanagawa'}, {id: 15, name:'Niigata'},
      {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'}, {id: 23, name:'Aichi'}, {id: 24, name:'Mie'},
      {id: 25, name:'Shiga Prefecture'}, {id: 26, name:'Kyoto Prefecture'}, {id: 27, name:'Osaka Prefecture'},
      {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'}, {id: 47, name:'Okinawa'}
  ]

end

I will enter in the hash format like this
Next association

product.rb


class product <ApplicationRecord

  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :prefecture

end

This time, it will be linked to the product table, so describe it in product.rb

Normally it is belongs_to, but for active_hash it becomes belongs_to_active_hash

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 prefecture'}, {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 prefecture'}, {id: 12, name:'Chiba prefecture'},
      {id: 13, name:'Tokyo'}, {id: 14, name:'Kanagawa'}, {id: 15, name:'Niigata'},
      {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'}, {id: 23, name:'Aichi'}, {id: 24, name:'Mie'},
      {id: 25, name:'Shiga Prefecture'}, {id: 26, name:'Kyoto Prefecture'}, {id: 27, name:'Osaka Prefecture'},
      {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'}, {id: 47, name:'Okinawa'}
  ]

  include ActiveHash::Associations
  has_many :users

end

The prefecture side is like this

Just display it! !