[RUBY] How to use active_hash! !!

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:

What is active_hash in the first place?

-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.

1. Install gem'active_hash'

Describe the following in the Gemfile of the application

gem 'active_hash'

Run bundle install in the terminal

$ bundle install

2. Create a model that inherits Active_Hash :: Base

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

3. Form an association

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

4. Display

When using collection_select in the form to register products, it can be displayed as follows.

Image from Gyazo


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

5. When you want to display the registered information

= @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

How to use active_hash! !!
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use java class
How to use Swift UIScrollView
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)
How to use scope (JSP & Servlet)
How to use the include? method