Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

store_model

> 后端框架
Open source

Work with JSON-backed attributes as ActiveRecord-ish models

1.2K stars0 likes0 views
WebsiteGitHub

About

Work with JSON-backed attributes as ActiveRecord-ish models

StoreModel

StoreModel gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes.

  • Powered with Attributes API. You can use a number of familiar types or write your own
  • Works like ActiveModel. Validations, enums and nested attributes work very similar to APIs provided by Rails
  • 1️⃣ Follows single responsibility principle. Keep the logic around the data stored in a JSON column separated from the model
  • ‍♂️ Born in production.
ruby
class Configuration
  include StoreModel::Model

  attribute :model, :string
  enum :status, %i[active archived], default: :active

  validates :model, :status, presence: true
end

class Product   Read more about the motivation in the [Wrapping JSON-based ActiveRecord attributes with classes](https://evilmartians.com/chronicles/wrapping-json-based-active-record-attributes-with-classes) post

## Getting started

Start with creating a class for representing the hash as an object:

```ruby
class Configuration
  include StoreModel::Model

  attribute :model, :string
  attribute :color, :string
end

Attributes should be defined using Rails Attributes API. There is a number of types available out of the box, and you can always extend the type system.

Register the field in the ActiveRecord model class:

ruby
class Product 
  
    
  

Resulting in:

xml

In the controller:

ruby
def create
  @supplier = Supplier.from_value(supplier_params)
  @supplier.save
end

private

def supplier_params
  params.require(:supplier).permit(products_attributes: [:name])
end

ActiveAdmin Integration

If you're using ActiveAdmin, enable compatibility mode to make the has_many form helper work with StoreModel attributes:

ruby
# config/initializers/store_model.rb
StoreModel.config.active_admin_compatibility = true

Example usage:

ruby
# app/models/supplier.rb
class Supplier
  include StoreModel::Model

  attribute :title, :string
  attribute :address, :string
end

# app/models/product.rb
class Product < ApplicationRecord
  include StoreModel::NestedAttributes

  attribute :suppliers, Supplier.to_array_type
  accepts_nested_attributes_for :suppliers, allow_destroy: true
end

# app/admin/products.rb
ActiveAdmin.register Product do
  permit_params suppliers_attributes: [:title, :address, :_destroy]

  form do |f|
    f.has_many :suppliers, allow_destroy: true do |s|
      s.input :title
      s.input :address
    end
    f.actions
  end
end

Documentation

  1. Installation
  2. StoreModel::Model API:
  • Instantiation
  • Validations
  • Enums
  • Nested models
  • Unknown attributes
  • Empty attributes
  1. Array of stored models
  2. Hash of stored models
  3. One of
  4. Alternatives
  5. Defining custom types
  6. Disabling Parent Tracking

Credits

Initially sponsored by Evil Martians.

License

The gem is available as open source under the terms of the MIT License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecordhacktoberfestjsonrails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架