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 閱讀(288) 評論(0)  編輯  收藏 所屬分類: ruby rails

          主站蜘蛛池模板: 泽普县| 句容市| 望谟县| 天津市| 额尔古纳市| 临汾市| 会泽县| 高阳县| 阿尔山市| 峡江县| 吴川市| 涪陵区| 乐昌市| 广水市| 武鸣县| 增城市| 龙泉市| 宁德市| 伊宁市| 那曲县| 胶州市| 家居| 昔阳县| 剑河县| 甘南县| 精河县| 朝阳县| 昌都县| 巴彦县| 临武县| 平安县| 吴桥县| 常熟市| 屏南县| 玉林市| 简阳市| 旺苍县| 昭平县| 仙桃市| 舞钢市| 大田县|