- Active record pattern
In
computer science , the active record pattern is a design pattern frequently found in software that stores its data inrelational database s. It was named byMartin Fowler in his book "Patterns of Enterprise Application Architecture".Active record is an approach to accessing data in a
database . Adatabase 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. Thewrapper class implementsaccessor 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 . Typicallyforeign key relationships will be exposed as an object instance of the appropriate type via a property.Implementations of Active Record can be found in various
framework s for many programming environments. For example, if in a database there is a tableparts
with columnsname
(string type) andprice
(number type), and the Active Record pattern is implemented in the classPart
, the following pseudo-code:part = new Part() part.name = "Sample part" part.price = 123.45 part.save()
will create a new row in the
parts
table with the given values, and is roughly equivalent to theSQL commandConversely, the class can be used to query the database:
b = Part.find_first("name", "gearbox")
This will create a new
Part
object based on the first matching row from theparts
table whosename
column has the value "gearbox". The SQL command used would beSee also
*
Object-relational mapping
Wikimedia Foundation. 2010.