[RUBY] I tried to explain Active Hash

ActiveHash By directly describing the unchanged data such as occupation selection in the model file, the data can be handled without saving it in the database. In other words, by using Active_Hash, ActiveRecord methods can be used for the unchanged data described directly in the model file.

Introduction method



gem 'active_hash'

After writing, execute bundle install.

Things to keep in mind when creating a model

In conclusion, use --skip-migration. The reason is not to create a database. That is, the migration file is no longer needed. Note that if you use the rails g model command here, a migration file will be created at the same time.

%rails g model model name--skip-migration

ActiveHash::Base You can use the same methods as ActiveRecord. In other words, by inheriting ActiveHash :: Base, ActiveRecord methods can be used for the objects defined in the model.


class ShippingFee < ActiveHash::Base
  self.data = [
    { id: 0, name: '---' },
    { id: 1, name: 'Cash on delivery(Buyer burden)' }, { id: 2, name: 'postage included(Exhibitor burden)' }

Image of creating a table with self.data. The data is stored in an array in hash format.

belongs_to_active_hash Normally, it will be belongs_to: model name, but if you want to set an association for a model created using ActiveHash, use belongs_to_active_hash method.

collection_select Method that can display data in pull-down format

Description order Details Concrete example
First argument Method name Column name
Second argument object Specification of array data
Third argument id Reference DB column name
Fourth argument name Actual column name
Fifth argument prompt What you want to display at the top of the pull-down
option name of the class --


<%= f.collection_select(:shipping_fee_id, Shipping_fee.all, :id, :name, {}, {class:"select-box", id:"item-shipping-fee-status"}) %>

Implementation example

I've listed some typical ones ·Prefectures ・ Occupation selection ・ Closed question (like answering yes or no) ·questionnaire ・ Category ・ Product status

Personally, I felt that this would greatly reduce the stress on the user side and that it would be possible to collect information efficiently. It may be good to use it when you want to ask the other party for their opinions.


Thank you for reading the article so far.

ActiveHash is also incorporated into things that you use casually. It's easy to implement, so I wanted to use it positively.

I would like to continue to provide useful information as I continue to study, so I look forward to working with you.

Thank you very much for reading the article so far!

Recommended Posts

I tried to explain Active Hash
I tried to explain the method
roman numerals (I tried to simplify it with hash)
I tried to chew C # (indexer)
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried to chew C # (basic of encapsulation)
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
I tried to output multiplication table in Java
I tried to link JavaFX and Spring Framework.
I tried to set tomcat to run the Servlet.
I tried to develop an application in 2 languages
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
I tried what I wanted to try with Stream softly.
[I tried] Spring tutorial
I tried to implement file upload with Spring MVC
I tried to organize the cases used in programming
I tried to read and output CSV with Outsystems
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java
I tried running Autoware
I tried to develop a ramen shop sharing website.
I tried using Gson