[Ruby] [RubyOnRails] What kind of data should active_hash be used for?

3 minute read


When developing an application with Rails, if you try to implement a form between levels that will exist in the real world to some extent, you will inevitably reach the existence of active_hash.

I thought that it would be convenient and easy to reach, but as I proceeded with various tasks, I gradually decided how to think when to use it and when it was not, so it is a memo to keep. ..


We are in the following environment.

ruby 2.6.5 rails 6.0.3

Data that should use #active_hash

From the conclusion, I wonder if the following idea is okay. ** ① Use for less important data ** ** ② Use for static data ** ** ③ Use for data that does not have complicated structure **

From the conclusion, it is said that it is the above two, but it is written everywhere, but I was not aware of ② or did not think about ③ in the first place It is the shape that came. Details will be described later.

What is active_hash?

This articleandthisarticleareveryeasytounderstand.Thankstoyou,Iwasabletointroduceitwithoutmuchtrouble.(Alwaysalwaysupsetmyancestors)

By using gem active_hash, you can create static data that can be used like Active Record without creating a table. (By the way, the latest version of gem has reduced the number of allowable description methods, so be careful when introducing it)

One example that is often used is prefecture data. The actual description is as follows.


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'}

Just create a file like this in your model. Very simple. id: and name: are the column names in the table. After that, you can use it simply by writing the association with the description for active_hash.


class Item <ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :prefecture

This time, I wanted to have a perfection_id in the item table, so I will write it in the model as above. It is not necessary to write the association on the active hash side. Now you can use it to some extent as if you made a table. Easy and convenient.

Static data, uncomplicated data?

static data

When I heard that it was static data, at first I was aware of data that the client side does not increase and data that is prepared on the server side.

However, as I proceeded, I thought that this recognition was insufficient. Specifically, even if it is data prepared on the server side, if it is increasing and increasing due to usage changes, or the value will change later ** It can not be said to be static ,When**

As you can see from the above writing method, each value of the active hash is in the file, so updating this is tedious and risky (analog work)

Thinking so far, I finally understood the reason why prefectures appear in active_hash usage examples. Prefectures don’t increase due to client side or server side reasons.

Complex data

There was a section that came to my mind when I first knew this. I wanted to create a many-to-many association, but it was difficult with active hash, so it was a correct recognition.

Also, it seems that active hash cannot be used because tables with hierarchical structure are complicated. There was a lack of recognition.

At the end

I was able to get the impression that the meaning of the word “static data” could not be captured correctly during the development process. After all, I do not understand unless I try basics. I have to move my hands.