I tried using active_hash when creating flea market apps at a certain programming school, so please take a look! !! This article was written by a beginner, but I hope you find it helpful: pray_tone2:
-It is not as important as saving it as data in the DB. ・ Basically, it is not changed. -It can handle hashes that collect read-only information like Active_Record.
Describe the following in the Gemfile of the application
gem 'active_hash'
Run bundle install in the terminal
$ bundle install
Create your own model that inherits ActiveHash :: Base.
This time, we will create a prefecture model in which active_hash is often used.
I make it by myself like app / models / prefecture.rb.
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
Since we are creating a flea market app this time, we will use active_hash to display the prefectures when registering the delivery address of the product. We will form an association in product.rb.
product.rb
class Product < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :prefecture
end
When using collection_select in the form to register products, it can be displayed as follows.
= f.collection_select :prefecture_id, Prefecture.all, :id, :name, {prompt:"Please select"}, {class:""}
= @product.prefecture.name
You can use active_hash like this! !!
Thank you for watching to the end: pray_tone2:
I hope you find it helpful: thumbs up_tone2:
Recommended Posts