Working with a Database: Active Record
(Page 1 of 4 )
If you're interested in learning more about the Rails framework, keep reading. This three-part article series takes an in-depth look at one of its more important aspects, the Active Record. This article is excerpted from chapter four of the book
Beginning Rails: From Novice to Professional, written by Jeffrey Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
In the previous chapter, we took a whirlwind tour through creating a basic Rails application using the built-in scaffolding feature. We sketched out a basic model for our sample application, an event manager, and created the project databases. We used the built-in web server to deploy the application locally, and practiced adding and managing events from the web browser. In this chapter, we’re going to take a more in-depth look at how things work, starting with what is arguably the most important part of Rails: Active Record.
You might recall from Chapter 1 that Active Record is the Ruby library that handles database abstraction and interaction for Rails. In fact, whether you realized it or not, in the previous chapter, all the access to the database—adding, editing, and deleting events—happened through the magic of Active Record.
Active Record is the name given to the object-relational mapping library that ships with Rails. If you’re not sure what exactly object-relational mapping is, don’t worry. By the end of this chapter, you will know. For now, it’s best if you think of Active Record as being an intermediary that sits between your code and your database, allowing you to work with data effectively and naturally. When you use Active Record, you communicate with your database using pure Ruby code. Active Record translates the Ruby you write into a language that databases can understand.
The purpose of this chapter is to teach you how to use Active Record to talk to your database and perform basic operations. We’ll introduce you to the concepts you need to know about communicating with databases and object-relational mapping. Then we’ll take a look at Active Record and walk you through the techniques you need to know to effectively work with a database from Rails. If you don’t have a lot of database experience under your belt, you needn’t worry. You’ll find that working with databases through Active Record is a painless and even enjoyable experience. If you’re an experienced database guru, you’ll find that Active Record is an intelligent and efficient way to perform database operations without the need for low-level database-specific commands.
Introducing Active Record: Object-Relational Mapping on Rails The key feature of Active Record is that it maps tables to classes, table rows to objects, and table columns to object attributes. This practice is commonly known as object-relational mapping, or ORM for short. To be sure, Active Record isn’t the only ORM in existence, but it may well be the easiest to use of the bunch.
One of the reasons that Active Record is so easy to use is that there is almost no configuration required to have it map a table to a class. You just need to create a Ruby class that’s named after the table you want to map and extend the Active RecordBaseclass.
class Event < ActiveRecord::Base
end
Notice the part that reads< ActiveRecord::Base. That less-than sign indicates that the class on the left is a subclass of the one on the right. In Ruby, when you extend a class like this, you automatically gain access to all the functionality in the parent class. There’s a lot of code in theActiveRecord::Baseclass, but we don’t need to look at it. Our class merely extends it, and our work is finished.
Assuming Active Record knows how to find our database and that we have a table calledevents(note that the table name is plural while the class name is singular), the table is automatically mapped. If oureventstable contained the fieldstitle,location, andoccurs_on, we could do this:
event = Event.new
event.title = "BBQ at Hampton's House" event.location = "471 230 King Street" event.occurs_on = "2006-10-13"
event.save
Those five lines would write a new record to theeventstable. That’s a lot of ability we gained just by the simple act of subclassing! And that’s what we mean when we say that Active Record is easy to use. Notice how the table’s fields (title,location, andoccurs_on) can be read and written to using methods on the object we created (event). And we didn’t need to tell Active Record what our fields were named, or even that we had any fields. It figured that out all on its own. Of course, Active Record doesn’t just let you create new records. It can also read, update, and delete records, plus a lot more.
Active Record is database-agnostic, so it doesn’t care which database software you use, and it supports nearly every database out there. Since it’s a high-level abstraction, the code you write remains the same no matter which database you’re using. For the record (no pun intended), we’ll be using MySQL. As explained in Chapter 2, MySQL is open source, easy to use, fast, and arguably the most popular database used for Rails development. (Along with the MySQL site,http://mysql.com, the Wikipedia entry on MySQL is an excellent resource:http://en.wikipedia.org/wiki/Mysql.)
Next: What About SQL? >>
More Ruby-on-Rails Articles
More By Apress Publishing