Active record pattern

From Wikipedia, the free encyclopedia
  (Redirected from ActiveRecord (Rails))
Jump to navigation Jump to search

In software engineering, the active record pattern is an architectural pattern found in software that stores in-memory object data in relational databases. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture.[1] The interface of an object conforming to this pattern would include functions such as Insert, Update, and Delete, plus properties that correspond more or less directly to the columns in the underlying database table.

The active record pattern is an approach to accessing data in a database. A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database. When an object is updated, the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.

This pattern is commonly used by object persistence tools and in object-relational mapping (ORM). Typically, foreign key relationships will be exposed as an object instance of the appropriate type via a property.


Implementations of the concept can be found in various frameworks for many programming environments. For example, if in a database there is a table parts with columns name (string type) and price (number type), and the Active Record pattern is implemented in the class Part, the pseudo-code

part = new Part() = "Sample part"
part.price = 123.45

will create a new row in the parts table with the given values, and is roughly equivalent to the SQL command

INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);

Conversely, the class can be used to query the database:

b = Part.find_first("name", "gearbox")

This will find a new Part object based on the first matching row from the parts table whose name column has the value "gearbox". The SQL command used might be similar to the following, depending on the SQL implementation details of the database:

SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1; -- MySQL or PostgreSQL


ColdFusion has an open source implementation of the active record pattern.

The ColdFusion on Wheels framework has an implementation of the active record pattern. It is open source and has the added advantage of requiring no complex configuration.


PHP ActiveRecord is one open-source library designed to fulfill the active record pattern.[2]

Several open-source PHP frameworks also bundle their own ORM implementing the active record pattern. Most implementations support relationships, behaviors, validation, serialization and support for multiple data sources.

  • Maghead, a high performance database framework written in pure PHP, contains a class/query method generator to boost the performance. It mixes the active record pattern with repository pattern.
  • Boiler, an MVC framework for PHP, contains a set of tools for auto-generation of active record models.[3] The project, designed for data-centered projects, aims to automate as much of the development process as possible,[4] using Apache Ant. Although a new addition to Open Source market,[5] the project is already in use in many live applications, both commercially and open. The framework currently only supports MySQL though the developers have reported some commercial work in Postgres.
  • Cygnite PHP Framework's default database layer implements Active Record pattern which closely resemble with Ruby on Rails.[6]
  • Eloquent ORM its included with Laravel. Eloquent implements the active record pattern, closely resembling that of Ruby on Rails [7]
  • CakePHP's ORM implements the active record pattern,[8] but as of version 2.x queries return arrays of data, with levels of related data as required. Version 3.0 uses objects and offers a hybrid implementation of ActiveRecord and Datamapper patterns.
  • Lithium's ORM implements active record.
  • Symfony's default database layer and ORM "Doctrine" does not implement active record but rather a data mapper approach.
  • CodeIgniter has a query builder it calls "ActiveRecord", but which does not implement the Active Record pattern. Instead, it implements what the user guide refers to as a modified version of the pattern. The Active Record functionality in CodeIgniter can be achieved by using either CodeIgniter DataMapper library or CodeIgniter Gas ORM library.
  • Yii's ORM also implements the active record pattern.[9]
  • Propel also implements the active record pattern.[10]
  • Paris is A lightweight Active Record implementation for PHP5, built on top of Idiorm.[11]
  • Laravel is also a framework built around these same principles.


The Ruby library ActiveRecord implements ORM. It creates a persistable domain model from business objects and database tables, where logic and data are presented as a unified package. It largely simplifies object-record mapping by assuming homogeneous identification method (i.e. Surrogate key) by virtue of convention over configuration. ActiveRecord adds inheritance and associations to the pattern above, solving two substantial limitations of that pattern. A set of macros acts as a domain language for the latter, and the Single Table Inheritance pattern is integrated for the former; thus, ActiveRecord increases the functionality of the active record pattern approach to database interaction. ActiveRecord is the default ‘model’ component of the model-view-controller web-application framework Ruby on Rails, and is also a stand-alone ORM package for other Ruby applications. In both forms, it was conceived of by David Heinemeier Hansson, and has been improved upon by a number of contributors.[12]

Other ORMs have been released since ActiveRecord first appeared. For example, DataMapper and Sequel. These ORMs include improvements over the original ActiveRecord framework.[neutrality is disputed] As a response to their release and adoption by the Rails community, Ruby on Rails v3.0 became independent of an ORM system, so that Rails users can instead use DataMapper or Sequel as their ORM.


Django, one of Python's many web frameworks, uses the Active Record pattern for its ORM.[13]


The Java language implements the Active Record pattern via the ActiveJDBC library. ActiveJDBC is an implementation of Active Record design pattern inspired by Ruby on Rails ActiveRecord. ActiveJDBC is lightweight, fast, small and does not require any configuration.

ActiveJPA and jOOQ (for Java Object Oriented Querying) implements the Active record pattern, combining active records with source code generation and a querying DSL similar to SQL allowing for retrieving active records using complex SQL statements.

The Play framework is a Java web framework which implements the Active Record pattern, using ideas from Ruby on Rails.

JActiveRecord is yet another library providing easy ORM mapping for Java, inspired by Ruby on Rails ActiveRecord but more focused on Java's type-safety.


The Dart language implements the Active Record pattern via the Dartabase Migration and Dartabase Model packages

Dartabase Migration Serverside Database Object Models for simple data manipulation currently supporting MySQL/PGSQL inspired by Ruby on Rails Migration

Dartabase Model Serverside Database migration for simple version controlled database structure manipulation currently supporting MySQL/PGSQL inspired by Ruby on Rails Model now console and GUI (build in Polymer 1.0)

Other languages[edit]

There are several open source implementations of the Active Record pattern in other languages, including JavaScript (e.g., ActiveJS's Active Record[14]), Perl (DBIx::Class), ActionScript, Haxe (SPOD[15]), C#,[16] Objective-C[17] and Scala.[18]



Due to the coupling of database interaction and application logic when using the active record pattern, unit testing an active record object without a database becomes difficult.[citation needed] The negative effects on testability in the active record pattern can be minimized by using mocking or dependency injection frameworks to substitute the real data tier with a simulated one.[citation needed]

Single responsibility principle and separation of concerns[edit]

Another critique of the active record pattern is that, also due to the strong coupling of database interaction and application logic, an active record object does not follow the single responsibility principle and separation of concerns as opposed to multitier architecture which properly addresses these practices. Because of this, the active record pattern is best and most often employed in simple applications that are all forms-over-data with CRUD functionality, or only as one part of an architecture. Typically that part is data access and why several ORMs implement the active record pattern.

Distributed systems[edit]

Record based patterns work poorly in distributed systems especially where concurrency is impossible ( eg offline ). i.e. 2 updates both may have 1 field that is correct but only one of the 2 records can win.[clarification needed]

See also[edit]


  1. ^ Fowler, Martin (2003). Patterns of enterprise application architecture. Addison-Wesley. ISBN 978-0-321-12742-6.
  2. ^ "PHP ActiveRecord". Archived from the original on 2010-02-08. Retrieved 2009-10-23.
  3. ^ "Boiler Documentation: Models".
  4. ^ "YouTube demonstration of automation tools".
  5. ^ "Open Source Code, hosted on Github".
  6. ^ "Cygnite Framework Documentation: Active Record".
  7. ^ "Laravel Documentation: Eloquent ORM".
  8. ^ "CakePHP (most popular PHP framework)". Stanford University Wiki.
  9. ^ "The Definitive Guide to Yii: Active Record".
  10. ^ "Propel: Active Record Reference".
  11. ^ "Github: j4mie/paris".
  12. ^ "Ruby Active Record". Retrieved 2013-04-12.
  13. ^ Retrieved 2017-06-28. Missing or empty |title= (help)
  14. ^ "ActiveJS Active Record". Archived from the original on 2011-09-30. Retrieved 2011-07-28.
  15. ^ "SPOD Macros". Archived from the original on 2012-11-30. Retrieved 2013-01-09.
  16. ^ "Castle ActiveRecord". Retrieved 14 January 2014.
  17. ^ "Objective Record". Retrieved 14 January 2013.
  18. ^ "Scala Active Record". Retrieved 4 December 2013.

External links[edit]