Module: ActiveModelPersistence::Persistence

Extended by:
ActiveSupport::Concern
Includes:
Indexable, PrimaryKey, PrimaryKeyIndex
Defined in:
lib/active_model_persistence/persistence.rb

Overview

This mixin adds the ability to store and manage Ruby objects in an in-memory object store. These objects are commonly called ‘models’.

Examples:

class ModelExample
  include ActiveModelPersistence::Persistence
  attribute :id, :integer
  attribute :name, :string
  index :id, unique: true
  validates :id, presence: true
end

# Creating a model instance with `.new` does not save it to the object store
#
m = ModelExample.new(id: 1, name: 'James')
m.new_record? # => true
m.persisted? # => false
m.destroyed? # => false

# `save` will save the object to the object store
#
m.save
m.new_record? # => false
m.persisted? # => true
m.destroyed? # => false

# Once an object is persisted, it can be fetched from the object store using `.find`
# and `.find_by_*` methods.
m2 = ModelExample.find(1)
m == m2 # => true

m2 = ModelExample.find_by_id(1)
m == m2 # => true

# `destroy` will remove the object from the object store
#
m.destroy
m.new_record? # => true
m.persisted? # => false
m.destroyed? # => true

Defined Under Namespace

Modules: ClassMethods