[RUBY] [Rails] About active hash

What is Active Hash?

A gem that allows you to manipulate hashed values in the same way as Active Record Convenient because you can also paste Relation

Basic usage

You can write as follows

app/models/plan.rb


class Plan < ActiveHash::Base
  self.data = [
    {id: 1, name: "free"},
    {id: 2, name: "silver"},
    {id: 3, name: "gold"},
  ]
end

class Plan < ActiveHash::Base
  self.data = [
    {id => 1, name => "free"},
    {id => 2, name => "silver"},
    {id => 3, name => "gold"},
  ]
end

class Plan < ActiveHash::Base
  fields :name
  add id: 1, name: "free"
  add id: 2, name: "silver"
  add id: 3, name: "gold"
end

Terminal


pry(main)>Plan.first
=> #<Plan:0x00007ffd3d2c43f8 @attributes={:id=>1, :name=>"free"}>

If you describe the following in the model to which you want to paste the relation, you can manipulate the data using the association. There is nothing to add to the model side of ActiveHash
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to_active_hash :Active Hash model name

reference https://qiita.com/DON4024/items/78edb7a309ee96766952 https://qiita.com/Toman1223/items/8633142312bfa886d50b

Recommended Posts

[Rails] About active hash
[Rails] Introducing Active Hash
About Rails 6
gem active_hash About active hash
About Rails routing
About Active Storage
[About double hash]
[Rails] About ActiveJob ,!
About Rails controller
About RSpec (Rails)
[Ruby on Rails] About Active Record callbacks
[Rails] About migration files
[Rails 6] About main gems
About DelegationError (Active Storage)
About rails application server
About rails kaminari pagination
About rails version specification
Utilization of Active Hash
MEMO about Rails 6 series
[Rails] About Slim notation
Pitfalls of Active Hash
[Beginner] About Active Storage
[rails] About devise defaults
Rails: About partial templates
About rails strong parameters
[Beginner] About Rails Session
Table design (using Active Hash)
about the where method (rails)
[Ruby on Rails] about has_secure_password
About naming Rails model methods
[Rails] About scss folder structure
[Rails] About Rspec response test
About Rails scraping method Mechanize
What is Rails Active Record?
[Rails] Implemented a pull-down search function for Active Hash data
About the behavior of ruby Hash # ==
hash
[Rails] About the Punk List function
Hash
About the symbol <%%> in Rails erb
[Rails] About implementation of like function
[Rails] About helper method form_with [Basic]
[Rails] How to use Active Storage
About =
Consideration about Rails and Clean Architecture
I tried to explain Active Hash
[Ruby on Rails] About bundler (for beginners)
[Rails 6.0] About batch saving of multiple records
Rails Active Storage shrinks images before uploading
[Rails] About local: true described in form_with
Rails: A little summary about data types