[RAILS] About active_hash

What is active_hash?

A type of gem that allows hash data to be used in the same way as ActiveRecord. Data that is basically unchanged, such as prefectures, does not need to be saved in the database, and if the data is written directly in the view file, it lacks readability. Use active_hash in such situations.


Creating a sports article posting app using a simple Active Hash (CSS is omitted this time)

Introduced Active Hash

gem 'active_hash'

Create a model

Next, create a model. This time, we will create two models, a sports model and a genre model. First, create a sports model with rails g model.

rails g model sports

Next, create a genre model.

rails g model genre --skip-migration

This time --skip-migration is an option to not generate the migration file when creating the model file.

Next, edit genre.rb. At this time, inherit the ActiveHash :: Base class. By inheriting ActiveHash :: Base, you can use methods similar to ActiveRecord. Result Suppose you write the following object.

class Genre < ActiveHash::Base
 self.data = [
   { id: 1, name: '--' },
   { id: 2, name: 'baseball' },
   { id: 3, name: 'Football' },
   { id: 4, name: 'tennis' },
   { id: 5, name: 'Table tennis' },
   { id: 6, name: 'basketball' },
   { id: 7, name: 'land' },
   { id: 8, name: 'Sports' }

Genre data is stored in an array in hash format.

Also, by adding a column called genre_id to the sports model migration file and managing the genre model (ActiveHash) id as an external key in the sports table, you will be able to acquire the genre associated with that article. ..

class CreateSports < ActiveRecord::Migration[6.0]
 def change
   create_table :sports do |t|
     t.string     :title        , null: false
     t.text       :text         , null: false
     t.integer    :genre_id     , null: false     t.timestamps
$ rails db:create
$ rails db:migrate

Association settings

Since active_hash has a belongs_to_active_hash method, Describe this method in sports.rb and define the association.

class Sports < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :genre

Set validation

Add validation to sports.rb earlier.

class Sports < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :genre

  #Prevent empty posts from being saved
  validates :title, :text, presence: true

  #The genre selection is "--When "", it cannot be saved
  validates :genre_id, numericality: { other_than: 1 } 

Routing settings

Rails.application.routes.draw do
  root to: 'sportses#index'
  resources :sportses

Create controller and view

rails g controller sportses index new

Edit controller


class ArticlesController < ApplicationController
  def index
    @sportses = Sports

  def new

  def create
    @sports = Sports.new(sports_params)
    if @sports.save
      redirect_to root_path
      render :new


  def sports_params

Edit view


<h1>List of articles</h1>
<%= link_to "Post", new_sports_path, class:"post" %>

<% @sportses.each do |sports| %>
  <div class="sports">
    <div class="sports-genre">
      <%= sports.genre.name %>
    <div class="sports-title">
      <%= sports.title %>
    <div class="sports-text">
      <%= sports.text %>
<% end %>

Implementation of pull-down bar

It is an implementation of the article posting screen.


<%= form_with model: @sports, url:sportses_path, local: true do |f| %>
  <div class="sports-box">
Post an article
    <%= f.text_field :title, class:"title", placeholder:"title" %>
    <%= f.text_area :text, class:"text", placeholder:"text" %>
    <%= f.collection_select(:genre_id, Genre.all, :id, :name, {}, {class:"genre-select"}) %>
    <%= f.submit "Post" ,class:"btn" %>
<% end %>

By using collection_select, you can use the active hash in pull-down format. collection_select exists from the first argument to the fifth argument, and the arguments are input in the following order. The data (array) of genre.rb is specified by Genre.all. The sport name actually selected by the user in the view is saved in the sports model as genre_id.

<%= form.collection_select(Column name to be saved,Array of objects,Items stored in columns,Column name displayed in the options,option,html option) %>

<%= f.collection_select(:genre_id, Genre.all, :id, :name, {}, {class:"genre-select"}) %>

At the end

For the first time, I made a full-scale output so far, so I think there were many parts that could not be reached. I will edit it to make it easier to understand, but by writing this article, I was able to sort out my understanding of active hashes.

Recommended Posts

About active_hash
About =
gem active_hash About active hash
About method.invoke
About Kotlin
About attr_accessor
About Hinemos
About inheritance
About params
About Docker
About form_for
About Spring ③
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About static
About exceptions
About scope
[Maven] About Maven
About exception handling
Review about bundler
About Java interface
[Java] About Java 12 features
About Rails routing
About cyclomatic complexity
About exception handling
About Ruby symbols
About array multiplication
[Java] About arrays
About HotSpot VM
About ruby ​​form
[Swift] About generics
About class inheritance.
About Spring AOP
About Ruby Hashes
About singular methods
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil
About keyword arguments
Chew about API
Something about java
Where about java
About HttpServlet () methods
About Java features
About ActiveRecord :: PendingMigrationError
About SIGNING_REGION: NoSuchFieldError
About the method
About standard classes
About Ruby arrays
About Ruby inheritance
About Java threads
About qiita_org commands