MagLev persistence with an ActiveRecord-like interface!
Ruby Shell JavaScript
Failed to load latest commit information.

MagLevRecord - a model interface for Rails

Build Status master Code Climate

ActiveRecord is the ORM commonly used when working with Rails and relational databases. When using MagLev, using an ORM is no longer necessary, you can just work with your objects as they are. Still, ActiveRecord::Base offers some nice features for working with your models (e.g. like Validations).

This gem offers all those features and bundles them in a comfortable way.


gem install maglevrecord

Basic Usage

Creating classes

Models, which you wish to persist, have to include MaglevRecord::Base or MaglevRecord::RootedBase. You can still inherit from whatever base class you want.

The difference between RootedBase and Base is, that RootedBase automatically stores the instances in Maglev::PERSISTENT_ROOT (persistence by reachability). The instances are stored in a hash, which contains all instances of the class.

When using Base you are responsible for making the object reachable from PERSISTENT_ROOT.

require 'maglev_record'

class Book
  include MaglevRecord::RootedBase

  attr_accessor :author, :title, :comments

Use attr_reader, attr_writer or attr_accessor to define your fields and use the generated getters and setters to access them. Do not access the instance variables directly, because we might need to do some background work when accessing and setting in future versions.


There is a significant difference between ActiveRecord and MaglevRecord: ActiveRecord features an object-wise saving mechanism, whereas MaglevRecord can only save all objects marked as persistent at once. That is the Smalltalk image idea: make some changes and then save the whole world.

book = => "A new book", :author => "The writer")
another_book = => "Another new book", :author => "The writer")

Important: When using MaglevRecord in a Rails app, unsaved changes are lost at the end of the request (die to an automatic request-wrapper).

Accessing model objects

Rooted model classes (ActiveRecord::RootedBase) implement Enumerable, so you can just use the normal collection API. You can use these methods for queries. Dynamic finders and indexes will be implemented later.

 book = Book.find { |b| == "The writer" }

Ordinary model classes (ActiveRecord::Base) do not provide a mechanism access such models directly. They are used only for referencing such models from other models, i.e. if the model is not reachable from a rooted model, the garbage collector will remove it after some time.

Deleting models

Rooted models can be deleted with delete.

Warning: This will only remove the model from the enumeration of all models. This model will not be removed until the last reachable reference to it is removed.


MaglevRecord supports model validation via ActiveModel.

require 'maglev_record'

class Book
  include MaglevRecord::Base

  validates :author, :presence => true
  validates :title,  :presence => true,
                     :length => { :minimum => 5 }

  attr_accessor :author, :title, :comments

Using book.valid? you can determine if a model is valid. Models are not automatically validated on global save.

Resetting objects

Use MaglevRecord.reset the reset all changed made since the last save or reset.

Migrating objects

Have a look at the demo application for migrations.

Further questions, improvements?

Feel free to fork, pull-request or ask via email at bp2012h1 [at] We are actively developing this and hope for feedback.