The goal of this article is to introduce the concepts behind ActiveRecord and the active record pattern to beginning programmers so that they can go on to successfully complete beginning tutorials without being confused by the terminology, etc.
What is ActiveRecord?
“Active record pattern” is an object-oriented paradigm for relational databases. (Nothing here will make sense if you don’t have any idea what that means, so go read up if you need to and then come back here when you’re done.)
Objects contain data and methods that can act on that data, so in your database there is a Class for the table, and an instance of that Class is a row in the table. The row/object has both data and some functions that can be used to act on that data.
ActiveRecord is a Ruby library that uses the active record pattern. It comes with the Ruby on Rails framework but is also available as a gem and can be used independently of Rails. It gives you access to handy methods for your Class/table to inherit and use, so you can easily interact with your database in an object-oriented way.
How it works/Examples
“Class methods are used to perform table-level operations, and instance methods perform operations on the individual rows.” - RubyLearning.com
In the example below, we define the table
users as the Class,
User. We also define the table
things as the Class
We make both classes children of ActiveRecord’s
Base class so that they can inherit methods like
column_for_attribute (and more!), which define the primary key field and return the column object for the named attribute, respectively.
class User < ActiveRecord::Base has_many :things end class Thing < ActiveRecord::Base belongs_to :user end james = user.create(:name => 'James')
As you might have guessed,
Base also includes the methods
belongs_to, which help to define the relationships between tables in your database.
And finally, we have created
james as an instance of the class
User, and ‘James’ will go in the
:name column of the
There is a lot more to do in order to make this code work, including defining the tables themselves (imagine that!), which you can easily learn about in one of many tutorials available on the web. I’ve included a couple of tutorials below.
By wrapping up data with the methods that can interact with them, the active record pattern informs the programmer as to what can be done with that data.
Inheritance increases functionality by creating access to a whole host of useful functions that do not have to be defined every time you create a new table/Class.
When you go to test your code to make sure it works, if your data and your methods are all wrapped together it is hard to test your functions without a database! To get around this you may need to create a testing database or pass your data as an argument. (See also Data Mapper vs Active Record.)
A similar-yet-different option to ActiveRecord in Ruby is Sequel, which is newer to the scene yet offers a few features developers might find appealing, such as a decreased need to use raw SQL in your queries.
Of course, Ruby isn’t the only language with libraries to implement active record architecture. Listing them all isn’t the goal of this article, but a quick Google search or look on Wikipedia will help you to learn more about them.
Active record isn’t the only object-oriented approach to relational databases, though. The data mapper pattern “moves data between objects and a database while keeping them independent of each other and the mapper itself.” (Martin Fowler)
Objects created with an active record approach contain methods including create, read, update, and delete (CRUD). But objects created with a data mapper pattern, have a separate “layer” that takes care of those actions. This relieves some of the pain of testing, as it allows you to more easily test your non-CRUD methods.
Expand your learning
This article cannot possibly cover all the ins and outs of active record pattern and ActiveRecord, but should give give you enough of a beginner’s understanding to go out and learn more on your own!