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

Introduction

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

I also arrived at it and thought that it was "convenient", but as I proceeded with various work, the way of thinking when it should be used and when it should not be decided little by little, so I will leave it as a reminder. ..

environment

We are doing it 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 way of thinking is okay. ** ① Use for less important data ** ** ② Used for static data ** ** ③ Used for data that does not have a complicated structure **

From the conclusion, it is written everywhere with the feeling that it is the above two, but I was not aware of (2) or did not think about (3) in the first place, so I learned while proceeding with development. It's a shape that has gone unnoticed. Details will be described later.

What is active_hash in the first place?

This article and This article are very easy to understand. Thanks to you, I was able to introduce it without much trouble. (Always, my ancestors don't get upset)

By using gem active_hash, you can create static data that can be used like an active record without creating a table. (By the way, the latest version of gem has less allowed description methods, so be careful when introducing it)

Prefectural data is often used as an example. The actual description is as follows.

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

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

item.rb


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

This time I wanted to have prefecture_id in the item table, so I will describe it in the model as above. There is no need to describe the association on the active hash side. Now you can use it in the same way as if you made a table. Easy and convenient.

What is static data or non-complex data?

Static data

When I heard that it was static data, I was initially aware of ** data prepared on the server side ** without increasing the client side.

However, as I proceeded, I thought that this recognition was insufficient. Specifically, even if the data is prepared on the server side, if it increases steadily due to usage changes or the value changes 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 solid in the file, so updating this is annoying and risky (analog work).

With that in mind, I finally understood the reason why prefectures appear in the examples of using active_hash. The number of prefectures does not increase due to client-side or server-side convenience.

Complex data

There was a section that came to my mind when I first learned about this. I wanted to have a many-to-many association, but it was difficult with active hashing, so it was a recognition that was not a mistake.

Also, ** tables with a hierarchical structure ** are complicated, so it seems that active hashes cannot be used. The recognition was missing everywhere.

At the end

As I proceeded with the development, I got the impression that the meaning of the word "static data" could not be grasped accurately. After all, you can't understand until you try the basics. I only have to move my hands.

Recommended Posts

[RubyOnRails] What kind of data should active_hash be used for?
[RubyOnRails] What kind of data should active_hash be used for?
Map keySet, values should not be used
[Java] When var should be used and when it should not be used
Introduction of jQuery-until it can be used (record executed in preparation for asynchronous communication) haml
[Rails] Prepare dummy data to be used for testing [Faker]
[Ruby] What is `!!` used for?
What kind of StringUtil is good
What kind of method is define_method?
Map keySet, values should not be used
What is the data structure of ActionText?
5 things new programmers should be aware of