Active Record
Active Record is the ORM layer supplied with Rails. It 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 relying on convention and starting with sensible defaults, Active Record minimizes the amount of configuration that developers perform. To illustrate this, here’s a program that uses Active Record to wrap
our orders table.
require 'active_record'
class Order < ActiveRecord::Base
end
order = Order.find(1)
order.discount = 0.5
order.save
This code uses the new Order class to fetch the order with an id of 1 and
modify the discount. (We’ve omitted the code that creates a database connection
for now.) Active Record relieves us of the hassles of dealing with
the underlying database, leaving us free to work on business logic.
But Active Record does more than that. As you’ll see when we develop our
shopping cart application, starting on page 43, Active Record integrates
seamlessly with the rest of the Rails framework. If a web form contains
data related to a business object, Active Record can extract it into our
model. Active Record supports sophisticated validation of model data, and
if the form data fails validations, the Rails views can extract and format
errors with just a single line of code.
Active Record is the solid model foundation of the Rails MVC architecture.
No comments:
Post a Comment