Prev | Current Page 40 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

Active Record adapters are basically
custom implementations of various parts of the Active Record library that abstract the proprietary
bits of each database system, such as connection details, so that the Active Record library
pretty much works the same regardless of the backend database system you are using. The most
popular and widely used of these adapters are now also directly included as part of the library
(we??™ll mention many of the contributors and developers later in this chapter when we cover
the specifics of each database adapter for Active Record).
Active Record Mostly Adheres to the ORM Pattern
The core concept of Active Record and other object relational mapping (ORM) libraries is that
relational databases can be represented reasonably in object-based code if you simply think of
database tables as classes, table rows as objects, and table fields as object attributes. Looking
at a quick example will help to explain this concept best, so assume we had something like the
following accounts table in some type of database:
CHAPTER 1 ?–  INTRODUCING ACTIVE RECORD 2
Accounts table
ID field (integer; auto-incremented; primary key)
Username field (text type field)
Password field (text type field)
Our Active Record Account class, or model as it??™s commonly referred to, would look something
like this:
Class Account < ActiveRecord::Base
end
And finally, throughout our Ruby or Rails code, we would create instances of account
objects like this:
# creates a new account object in memory and a new account record in our database
newacc = Account.


Pages:
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52