wash

Active Record Basics

Active Record is the object-relational mapping (ORM) layer supplied with
Rails. In this chapter, we’ll look at the basics of Active Record—connecting
to databases, mapping tables, and manipulating data. We’ll dig deeper
into the more advanced stuff in the next chapter.
Active Record closely follows the standard ORM model: tables map to
classes, rows to objects, and columns to object attributes. It differs from
most other ORM libraries in the way it is configured. By using a sensible
set of defaults, Active Record minimizes the amount of configuration that
developers perform. To illustrate this, here’s a program that uses Active
Record to wrap a table of orders in a MySQL database. After finding the
order with a particular id, it modifies the purchaser’s name and saves the
result back in the database, updating the original row.

require "rubygems"
require_gem "activerecord"
ActiveRecord::Base.establish_connection(:adapter => "mysql",
:host => "localhost", :database => "railsdb")
class Order < ActiveRecord::Base
end
order = Order.find(123)
order.name = "Dave Thomas"
order.save

That’s all there is to it—in this case no configuration information (apart
from the database connection stuff) is required. Somehow Active Record
figured out what we needed and got it right. Let’s have a look at how this
works.

14.1 Tables and Classes
When you create a subclass of ActiveRecord::Base, you’re creating something
that wraps a database table. By default, Active Record assumes that
the name of the table is the plural form of the name of the class. If the class
name contains multiple capitalized words, the table name is assumed to
have underscores between these words. Some irregular plurals are handled.
Class Name
Order
TaxAgency
Diagnosis
Batch
Table Name
tax_agencies
orders
batches
diagnoses
LineItem
Person
Datum
Quantity
Class Name
line_items
people
quantities
data
Table Name
These rules reflect DHH’s philosophy that class names should be singular
while the names of tables should be plural. If you don’t like this behavior,
you can disable it by setting a global flag in your configuration (the file
environment.rb in the config directory).
ActiveRecord::Base.pluralize_table_names = false
The algorithm used to derive the plural form of a table name is fairly simplistic.
It works in the majority of common cases, but if you have a class
named Sheep, it’ll valiantly try to find a table named sheeps. The assumption
that the table name and class names are related might also break
down if you’re operating with a legacy schema,2 where the table names
might otherwise force you to use strange or undesirable class names in
your code. For this reason, Active Record allows you to override the default
generation of a table name using the set_table_name directive.


14.2 Columns and Attributes
Active Record objects correspond to rows in a database table. The objects
have attributes corresponding to the columns in the table. You probably
noticed that our definition of class Order didn’t mention any of the columns
in the orders table. That’s because Active Record determines them dynamically
at runtime. Active Record reflects on the schema inside the database
to configure the classes that wrap tables.3
Our orders table might have been created with the following SQL.
File 6 create table orders (
id int not null auto_increment,
name varchar(100) not null,
email varchar(255) not null,
address text not null,
pay_type char(10) not null,
shipped_at datetime null,
primary key (id)
);

posted on 2006-05-10 11:12 wash 阅读(279) 评论(0)  编辑  收藏 所属分类: ruby rails


只有注册用户登录后才能发表评论。


网站导航: