More at rubyonrails.org: Overview | Download | Deploy | Code | Screencasts | Documentation | Ecosystem | Community | Blog

Active Record Basics

This guide is an introduction to Active Record. After reading this guide we hope that you’ll learn:

1 What is Active Record?

Active Record is the M in MVC – the model – which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

1.1 The Active Record Pattern

Active Record was described by Martin Fowler in his book Patterns of Enterprise Application Architecture. In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic is part of the object will educate users of that object on how to write to and read from the database.

1.2 Object Relational Mapping

Object-Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.

1.3 Active Record as an ORM Framework

Active Record gives us several mechanisms, the most important being the ability to:

  • Represent models and their data
  • Represent associations between these models
  • Represent inheritance hierarchies through related models
  • Validate models before they get persisted to the database
  • Perform database operations in an object-oriented fashion.

2 Convention over Configuration in Active Record

When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly true for ORM frameworks in general. However, if you follow the conventions adopted by Rails, you’ll need to write very little configuration (in some case no configuration at all) when creating Active Record models. The idea is that if you configure your applications in the very same way most of the times then this should be the default way. In this cases, explicit configuration would be needed only in those cases where you can’t follow the conventions for any reason.

2.1 Naming Conventions

By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the camelCase form, while the table name must contain the words separated by underscores. Examples:

  • Database Table – Plural with underscores separating words (e.g., book_clubs)
  • Model Class – Singular with the first letter of each word capitalized (e.g., BookClub)
Model / Class Table / Schema
Post posts
LineItem line_items
Deer deer
Mouse mice
Person people

2.2 Schema Conventions

Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.

  • Foreign keys – These fields should be named following the pattern table_id (e.g., item_id, order_id). These are the fields that Active Record will look for when you create associations between your models.
  • Primary keys – By default, Active Record will use an integer column named “id” as the table’s primary key. When using Rails Migrations to create your tables, this column will be automatically created.

There are also some optional column names that will create additional features to Active Record instances:

  • created_at – Automatically gets set to the current date and time when the record is first created.
  • created_on – Automatically gets set to the current date when the record is first created.
  • updated_at – Automatically gets set to the current date and time whenever the record is updated.
  • updated_on – Automatically gets set to the current date whenever the record is updated.
  • lock_version – Adds optimistic locking to a model.
  • type – Specifies that the model uses Single Table Inheritance
  • _count – Used to cache the number of belonging objects on associations. For example, a comments_count column in a Post class that has many instances of Comment will cache the number of existent comments for each post.

While these column names are optional they are in fact reserved by Active Record. Steer clear of reserved keywords unless you want the extra functionality. For example, “type” is a reserved keyword used to designate a table using Single Table Inheritance. If you are not using STI, try an analogous keyword like “context”, that may still accurately describe the data you are modeling.

3 Creating Active Record Models

It’s very easy to create Active Record models. All you have to do is to subclass the ActiveRecord::Base class and you’re good to go:

class Product < ActiveRecord::Base; end

This will create a Product model, mapped to a products table at the database. By doing this you’ll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. So, suppose that the products table was created using an SQL sentence like:

CREATE TABLE products (
   id int(11) NOT NULL auto_increment,
   name varchar(255),
   PRIMARY KEY  (id)
);

Following the table schema above, you would be able to write code like the following:

p = Product.new
p.name = "Some Book"
puts p.name # "Some Book"

4 Overriding the Naming Conventions

What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.

You can use the ActiveRecord::Base.set_table_name method to specify the table name that should be used:

class Product < ActiveRecord::Base
  set_table_name "PRODUCT"
end

If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the set_fixture_class method in your test definition:

class FunnyJoke < ActiveSupport::TestCase
  set_fixture_class :funny_jokes => 'Joke'
  fixtures :funny_jokes
  ...
end

It’s also possible to override the column that should be used as the table’s primary key using the ActiveRecord::Base.set_primary_key method:

class Product < ActiveRecord::Base
  set_primary_key "product_id"
end

5 Reading and Writing Data

CRUD is an acronym for the four verbs we use to operate on data: *C*reate, *R*ead, *U*pdate and *D*elete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.

5.1 Create

Active Record objects can be created from a hash, a block or have its attributes manually set after creation. The new method will return a new object while create will return the object and save it to the database.

For example, given a model User with attributes of name and occupation, the create method call will create and save a new record into the database:

user = User.create(:name => "David", :occupation => "Code Artist")

Using the new method, an object can be created without being saved:

user = User.new
  user.name = "David"
  user.occupation = "Code Artist"

A call to user.save will commit the record to the database.

Finally, passing a block to either create or new will return a new User object:

user = User.new do |u|
    u.name = "David"
    u.occupation = "Code Artist"
  end

5.2 Read

Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record.

# return all records
  users = User.all
# return the first record
  user = User.first
# return the first user named David
  david = User.find_by_name('David')
# find all users named David who are Code Artists and sort by created_at in reverse chronological order
  users = User.all(:conditions => { :name => 'David', :occupation => 'Code Artist'}, :order => 'created_at DESC')

You can learn more about querying an Active Record model in the “Active Record Query Interface”:“active_record_querying.html” guide.

5.3 Update

Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.

user = User.find_by_name('David')
  user.name = 'Dave'
  user.save

5.4 Delete

Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.

user = User.find_by_name('David')
  user.destroy

6 Validations

Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format and many more. You can learn more about validations in the Active Record Validations and Callbacks guide.

7 Callbacks

Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on. You can learn more about callbacks in the Active Record Validations and Callbacks guide.

8 Migrations

Rails provides a domain-specific language for managing a database schema called migrations. Migrations are stored in files which are executed against any database that Active Record support using rake. Rails keeps track of which files have been committed to the database and provides rollback features. You can learn more about migrations in the Active Record Migrations guide

Feedback

You're encouraged to help in keeping the quality of this guide.

If you see any typos or factual errors you are confident to patch please clone docrails and push the change yourself. That branch of Rails has public write access. Commits are still reviewed, but that happens after you've submitted your contribution. docrails is cross-merged with master periodically.

You may also find incomplete content, or stuff that is not up to date. Please do add any missing documentation for master. Check the Ruby on Rails Guides Guidelines guide for style and conventions.

Issues may also be reported in Github.

And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome in the rubyonrails-docs mailing list.