記事概要
Ruby on RailsのActiveHash(Gem)について、まとめる
前提
- Ruby on Railsでアプリケーションを作成している
- ActiveHashを用いて実装する「クラス」を、便宜上「モデル」と呼称する
サンプルアプリ(GitHub)
ActiveHashとは
変更されないデータをモデルファイル内に直接記述することで、データベースへ保存せずにデータを取り扱うことができるGem
ActiveHash::Base
あるモデル内(クラス内)でActiveHashを用いる際に必要となるクラス
ActiveHashのGemに定義されており、ActiveHash::Base
を継承することで、ActiveRecordと同じようなメソッドを使用できる
Gemのインストール手順
Gemfileの記述
手順①(モデルの作成)
- ActiveHashを利用するモデルを作成済みの場合は、手順②へ進む
- モデルを作成する
% rails g model [モデル名]
- マイグレーションファイルを編集する
db/migrate/20XXXXXXXXXXXX_create_articles.rb
class CreateArticles < ActiveRecord::Migration[7.0] def change create_table :articles do |t| t.string :title , null: false t.text :text # カラム名は、[ActiveHashのモデル名]_id # カラム型は、integer t.integer :genre_id , null: false t.timestamps end end end
- マイグレーションを実行し、テーブルを作成する
手順②(ActiveHashのモデル作成)
-
app/models
配下に、ActiveHashのモデルを手動作成する
app/models/[ActiveHashのモデル名].rb - 作成したActiveHashのモデルに
ActiveHash::Base
を継承する[ActiveHashのモデル名].rbclass モデル名 < ActiveHash::Base end
- データは、配列にハッシュ形式で格納する
app/models/genre.rb
class Genre < ActiveHash::Base self.data = [ { id: 1, name: '経済' }, { id: 2, name: '政治' }, { id: 3, name: '地域' }, { id: 4, name: '国際' }, { id: 5, name: 'IT' }, { id: 6, name: 'エンタメ' }, { id: 7, name: 'スポーツ' }, { id: 8, name: 'グルメ' }, { id: 9, name: 'その他' } ] end
手順③(アソシエーションを設定)
- ActiveHashを利用するモデルに、アソシエーションを設定する
-
extend ActiveHash::Associations::ActiveRecordExtensions
と記述して、moduleを取り込む -
belongs_to
を設定する
app/models/article.rbclass Article < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :genre end
-
- ActiveHashのモデルに、アソシエーションを設定する
- 保存データを使用(ビューなど)しない場合は、アソシエーションの定義は不要
- 保存データを使用(ビューなど)する場合は、必ずアソシエーションを定義
-
include ActiveHash::Associations
と記述して、moduleを取り込む -
has_many
を設定する
app/models/genre.rbclass Genre < ActiveHash::Base self.data = [ { id: 1, name: '経済' }, { id: 2, name: '政治' }, { id: 3, name: '地域' }, { id: 4, name: '国際' }, { id: 5, name: 'IT' }, { id: 6, name: 'エンタメ' }, { id: 7, name: 'スポーツ' }, { id: 8, name: 'グルメ' }, { id: 9, name: 'その他' } ] include ActiveHash::Associations has_many :articles end
-
手順④(バリデーションを設定)
ActiveHashを利用するモデルに対して、バリデーションを設定する
- 全項目NULLで保存できないように、最低1項目はNULL制約をつける
app/models/article.rb
class Article < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :genre #空の投稿を保存できないようにする validates :title, presence: true end
- ActiveHashを紐づけている項目は、数値の場合のみデータを保存する
app/models/article.rb
class Article < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :genre #空の投稿を保存できないようにする validates :title, presence: true #数値以外を保存できないようにする validates :genre_id, numericality: true end
手順⑤(ビューでの表示)
- データを表示する
アソシエーションが定義されている場合、単一レコード情報.ActiveHashを用いたモデル名.カラム名
で情報を取得できる<!-- コントローラーで、「@articles = Article.all」を定義 --> <% @articles.each do |article| %> <!-- ActiveHashで紐付けた情報を表示する --> <%= article.genre.name %> <% end %>
-
collection_select
メソッドを使用して、form_withへ追加する<%= form_with model: @article, url:articles_path, local: true do |f| %> <%= f.text_field :title, class:"title", placeholder:"タイトル" %> <%= f.text_area :text, class:"text", placeholder:"テキスト" %> <!-- ActiveHashのモデルデータを表示 --> <%= f.collection_select(:genre_id, Genre.all, :id, :name, {}, {class:"genre-select"}) %> <%= f.submit "投稿する" ,class:"btn" %> <% end %>
Ruby on Railsまとめ