百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

store_model

> 后端框架
开源

以 JSON 为基础的属性可作为 ActiveRecord-ish 模型进行处理

1.2K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

以 JSON 为基础的属性可作为 ActiveRecord-ish 模型进行处理

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyactiverecordhacktoberfestjsonrails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

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