Skip to content
Active Record without Database
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Active Entity

Active Entity is a Rails virtual model solution based on ActiveModel and it's design for Rails 6+.

Active Entity is forked from Active Record by removing all database relates codes, so it nearly no need to learn how to use.

About Virtual Model

Virtual Model is the model not backed by a database table, usually used as "form model" or "presenter", because it's implement interfaces of Active Model, so you can use it like a normal Active Record model in your Rails app.


Attribute declaration

class Book < ActiveEntity::Base
  attribute :title, :string
  attribute :tags, :string, array: true, default: []

Same usage with Active Record, Learn more.

One enhancement is array: true that transform the attribute to an array that can accept multiple values.

Nested attributes

Active Entity supports its own variant of nested attributes via the embeds_one / embeds_many macros. The intention is to be mostly compatible with ActiveRecord's accepts_nested_attributes functionality.

class Holiday < ActiveEntity::Base
  attribute :date, :date
  validates :date, presence: true

class HolidaysForm < ActiveEntity::Base
  embeds_many :holidays, reject_if: :all_blank


class Book < ActiveEntity::Base
  attribute :title, :string
  validates :title, presence: true

Supported Active Record validations:

Common validation options supported too.

subset validation

Because Active Entity supports array attribute, for some reason, we may want to test values of an array attribute are all included in a given set.

Active Entity provides subset validation to achieve that, it usage similar to inclusion or exclusion

class Steak < ActiveEntity::Base
  attribute :side_dishes, :string, array: true, default: []
  validates :side_dishes, subset: { in: %w(chips mashed_potato salad) }

uniqueness_in_embedding validation

Active Entity provides uniqueness_in_embedding validation to test duplicate nesting virtual record.

Argument key is attribute name of nested model, it also supports multiple attributes by given an array.

class Category < ActiveEntity::Base
  attribute :name, :string

class Reviewer < ActiveEntity::Base
  attribute :first_name, :string
  attribute :last_name, :string 

class Book < ActiveEntity::Base
  embeds_many :categories
  validates :categories, uniqueness_in_embedding: {key: :name}
  embeds_many :reviewers
  validates :categories, uniqueness_in_embedding: {key: [:first_name, :last_name]}


These Active Record feature also available in Active Entity


Same to Active Record I18n, the only different is the root of locale YAML is active_entity instead of activerecord

Read-only attributes

You can use attr_readonly :title, :author to prevent assign value to attribute after initialized.

You can use enable_readonly! and disable_readonly! to control the behavior.

Important: It's no effect with embeds or array attributes !!!


Most of Active Model plugins are compatible with Active Entity.

You need to include them manually.

Tested extensions:


Add this line to your application's Gemfile:

gem 'activeentity'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activeentity

Other awesome gems


  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


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

You can’t perform that action at this time.