Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/1-2-stable/activerecord/README

Revision 5257, 11.5 kB (checked in by david, 2 years ago)

Docfix (closes #5143)

  • Property svn:executable set to *
Line 
1 = Active Record -- Object-relation mapping put on rails
2
3 Active Record connects business objects and database tables to create a persistable
4 domain model where logic and data are presented in one wrapping. It's an implementation
5 of the object-relational mapping (ORM) pattern[http://www.martinfowler.com/eaaCatalog/activeRecord.html]
6 by the same name as described by Martin Fowler:
7
8   "An object that wraps a row in a database table or view, encapsulates
9        the database access, and adds domain logic on that data."
10
11 Active Record's main contribution to the pattern is to relieve the original of two stunting problems:
12 lack of associations and inheritance. By adding a simple domain language-like set of macros to describe
13 the former and integrating the Single Table Inheritance pattern for the latter, Active Record narrows the
14 gap of functionality between the data mapper and active record approach.
15
16 A short rundown of the major features:
17
18 * Automated mapping between classes and tables, attributes and columns.
19
20    class Product < ActiveRecord::Base; end
21    
22    ...is automatically mapped to the table named "products", such as:
23    
24    CREATE TABLE products (
25      id int(11) NOT NULL auto_increment,
26      name varchar(255),
27      PRIMARY KEY  (id)
28    );
29
30    ...which again gives Product#name and Product#name=(new_name)
31    
32   {Learn more}[link:classes/ActiveRecord/Base.html]
33
34
35 * Associations between objects controlled by simple meta-programming macros.
36
37    class Firm < ActiveRecord::Base
38      has_many   :clients
39      has_one    :account
40      belongs_to :conglomorate
41    end
42
43   {Learn more}[link:classes/ActiveRecord/Associations/ClassMethods.html]
44
45
46 * Aggregations of value objects controlled by simple meta-programming macros.
47
48    class Account < ActiveRecord::Base
49      composed_of :balance, :class_name => "Money",
50                  :mapping => %w(balance amount)
51      composed_of :address,
52                  :mapping => [%w(address_street street), %w(address_city city)]
53    end
54
55   {Learn more}[link:classes/ActiveRecord/Aggregations/ClassMethods.html]
56
57
58 * Validation rules that can differ for new or existing objects.
59
60     class Account < ActiveRecord::Base
61       validates_presence_of     :subdomain, :name, :email_address, :password
62       validates_uniqueness_of   :subdomain
63       validates_acceptance_of   :terms_of_service, :on => :create
64       validates_confirmation_of :password, :email_address, :on => :create
65     end
66
67   {Learn more}[link:classes/ActiveRecord/Validations.html]
68
69
70 * Acts that can make records work as lists or trees:
71
72     class Item < ActiveRecord::Base
73       belongs_to   :list
74       acts_as_list :scope => :list
75     end
76    
77     item.move_higher
78     item.move_to_bottom
79
80   Learn about {acts_as_list}[link:classes/ActiveRecord/Acts/List/ClassMethods.html], {the instance methods acts_as_list provides}[link:classes/ActiveRecord/Acts/List/InstanceMethods.html], and
81   {acts_as_tree}[link:classes/ActiveRecord/Acts/Tree/ClassMethods.html]
82  
83 * Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).
84
85    class Person < ActiveRecord::Base
86      def before_destroy # is called just before Person#destroy
87        CreditCard.find(credit_card_id).destroy
88      end
89    end
90
91    class Account < ActiveRecord::Base
92      after_find :eager_load, 'self.class.announce(#{id})'
93    end
94
95   {Learn more}[link:classes/ActiveRecord/Callbacks.html]
96
97
98 * Observers for the entire lifecycle
99
100    class CommentObserver < ActiveRecord::Observer
101      def after_create(comment) # is called just after Comment#save
102        Notifications.deliver_new_comment("david@loudthinking.com", comment)
103      end
104    end
105
106   {Learn more}[link:classes/ActiveRecord/Observer.html]
107
108
109 * Inheritance hierarchies
110
111    class Company < ActiveRecord::Base; end
112    class Firm < Company; end
113    class Client < Company; end
114    class PriorityClient < Client; end
115
116   {Learn more}[link:classes/ActiveRecord/Base.html]
117
118
119 * Transaction support on both a database and object level. The latter is implemented
120   by using Transaction::Simple[http://railsmanual.com/module/Transaction::Simple]
121
122     # Just database transaction
123     Account.transaction do
124       david.withdrawal(100)
125       mary.deposit(100)
126     end
127
128     # Database and object transaction
129     Account.transaction(david, mary) do
130       david.withdrawal(100)
131       mary.deposit(100)
132     end
133
134   {Learn more}[link:classes/ActiveRecord/Transactions/ClassMethods.html]
135
136
137 * Reflections on columns, associations, and aggregations
138
139     reflection = Firm.reflect_on_association(:clients)
140     reflection.klass # => Client (class)
141     Firm.columns # Returns an array of column descriptors for the firms table
142
143   {Learn more}[link:classes/ActiveRecord/Reflection/ClassMethods.html]
144
145
146 * Direct manipulation (instead of service invocation)
147
148   So instead of (Hibernate[http://www.hibernate.org/] example):
149
150      long pkId = 1234;
151      DomesticCat pk = (DomesticCat) sess.load( Cat.class, new Long(pkId) );
152      // something interesting involving a cat...
153      sess.save(cat);
154      sess.flush(); // force the SQL INSERT
155
156   Active Record lets you:
157
158      pkId = 1234
159      cat = Cat.find(pkId)
160      # something even more interesting involving the same cat...
161      cat.save
162
163   {Learn more}[link:classes/ActiveRecord/Base.html]
164
165
166 * Database abstraction through simple adapters (~100 lines) with a shared connector
167
168    ActiveRecord::Base.establish_connection(:adapter => "sqlite", :database => "dbfile")
169
170    ActiveRecord::Base.establish_connection(
171      :adapter  => "mysql",
172      :host     => "localhost",
173      :username => "me",
174      :password => "secret",
175      :database => "activerecord"
176    )
177
178   {Learn more}[link:classes/ActiveRecord/Base.html#M000081] and read about the built-in support for
179   MySQL[link:classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html], PostgreSQL[link:classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html], SQLite[link:classes/ActiveRecord/ConnectionAdapters/SQLiteAdapter.html], Oracle[link:classes/ActiveRecord/ConnectionAdapters/OCIAdapter.html], SQLServer[link:classes/ActiveRecord/ConnectionAdapters/SQLServerAdapter.html], and DB2[link:classes/ActiveRecord/ConnectionAdapters/DB2Adapter.html].
180
181
182 * Logging support for Log4r[http://log4r.sourceforge.net] and Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc]
183
184     ActiveRecord::Base.logger = Logger.new(STDOUT)
185     ActiveRecord::Base.logger = Log4r::Logger.new("Application Log")
186
187
188 == Simple example (1/2): Defining tables and classes (using MySQL)
189
190 Data definitions are specified only in the database. Active Record queries the database for
191 the column names (that then serves to determine which attributes are valid) on regular
192 object instantiation through the new constructor and relies on the column names in the rows
193 with the finders.
194  
195    # CREATE TABLE companies (
196    #   id int(11) unsigned NOT NULL auto_increment,
197    #   client_of int(11),
198    #   name varchar(255),
199    #   type varchar(100),
200    #   PRIMARY KEY  (id)
201    # )
202
203 Active Record automatically links the "Company" object to the "companies" table
204
205    class Company < ActiveRecord::Base
206      has_many :people, :class_name => "Person"
207    end
208
209    class Firm < Company
210      has_many :clients
211  
212      def people_with_all_clients
213       clients.inject([]) { |people, client| people + client.people }
214      end
215    end
216
217 The foreign_key is only necessary because we didn't use "firm_id" in the data definition
218  
219    class Client < Company
220      belongs_to :firm, :foreign_key => "client_of"
221    end
222
223    # CREATE TABLE people (
224    #   id int(11) unsigned NOT NULL auto_increment,
225    #   name text,
226    #   company_id text,
227    #   PRIMARY KEY  (id)
228    # )
229
230 Active Record will also automatically link the "Person" object to the "people" table
231
232    class Person < ActiveRecord::Base
233      belongs_to :company
234    end
235
236 == Simple example (2/2): Using the domain
237
238 Picking a database connection for all the Active Records
239
240    ActiveRecord::Base.establish_connection(
241      :adapter  => "mysql",
242      :host     => "localhost",
243      :username => "me",
244      :password => "secret",
245      :database => "activerecord"
246    )
247
248 Create some fixtures
249
250    firm = Firm.new("name" => "Next Angle")
251    # SQL: INSERT INTO companies (name, type) VALUES("Next Angle", "Firm")
252    firm.save
253
254    client = Client.new("name" => "37signals", "client_of" => firm.id)
255    # SQL: INSERT INTO companies (name, client_of, type) VALUES("37signals", 1, "Firm")
256    client.save
257
258 Lots of different finders
259
260    # SQL: SELECT * FROM companies WHERE id = 1
261    next_angle = Company.find(1)
262
263    # SQL: SELECT * FROM companies WHERE id = 1 AND type = 'Firm'
264    next_angle = Firm.find(1)   
265
266    # SQL: SELECT * FROM companies WHERE id = 1 AND name = 'Next Angle'
267    next_angle = Company.find(:first, :conditions => "name = 'Next Angle'")
268
269    next_angle = Firm.find_by_sql("SELECT * FROM companies WHERE id = 1").first
270
271 The supertype, Company, will return subtype instances
272
273    Firm === next_angle
274
275 All the dynamic methods added by the has_many macro
276
277   next_angle.clients.empty?  # true
278   next_angle.clients.size    # total number of clients
279   all_clients = next_angle.clients
280
281 Constrained finds makes access security easier when ID comes from a web-app
282
283    # SQL: SELECT * FROM companies WHERE client_of = 1 AND type = 'Client' AND id = 2
284    thirty_seven_signals = next_angle.clients.find(2)
285
286 Bi-directional associations thanks to the "belongs_to" macro
287
288    thirty_seven_signals.firm.nil? # true
289
290
291 == Examples
292
293 Active Record ships with a couple of examples that should give you a good feel for
294 operating usage. Be sure to edit the <tt>examples/shared_setup.rb</tt> file for your
295 own database before running the examples. Possibly also the table definition SQL in
296 the examples themselves.
297
298 It's also highly recommended to have a look at the unit tests. Read more in link:files/RUNNING_UNIT_TESTS.html
299
300
301 == Philosophy
302
303 Active Record attempts to provide a coherent wrapper as a solution for the inconvenience that is
304 object-relational mapping. The prime directive for this mapping has been to minimize
305 the amount of code needed to build a real-world domain model. This is made possible
306 by relying on a number of conventions that make it easy for Active Record to infer
307 complex relations and structures from a minimal amount of explicit direction.
308
309 Convention over Configuration:
310 * No XML-files!
311 * Lots of reflection and run-time extension
312 * Magic is not inherently a bad word
313
314 Admit the Database:
315 * Lets you drop down to SQL for odd cases and performance
316 * Doesn't attempt to duplicate or replace data definitions
317
318
319 == Download
320
321 The latest version of Active Record can be found at
322
323 * http://rubyforge.org/project/showfiles.php?group_id=182
324
325 Documentation can be found at
326
327 * http://ar.rubyonrails.com
328
329
330 == Installation
331
332 The prefered method of installing Active Record is through its GEM file. You'll need to have
333 RubyGems[http://rubygems.rubyforge.org/wiki/wiki.pl] installed for that, though. If you have,
334 then use:
335
336   % [sudo] gem install activerecord-1.10.0.gem
337
338 You can also install Active Record the old-fashion way with the following command:
339
340   % [sudo] ruby install.rb
341
342 from its distribution directory.
343
344
345 == License
346
347 Active Record is released under the MIT license.
348
349
350 == Support
351
352 The Active Record homepage is http://www.rubyonrails.com. You can find the Active Record
353 RubyForge page at http://rubyforge.org/projects/activerecord. And as Jim from Rake says:
354
355    Feel free to submit commits or feature requests.  If you send a patch,
356    remember to update the corresponding unit tests.  If fact, I prefer
357    new feature to be submitted in the form of new unit tests.
358
359 For other information, feel free to ask on the ruby-talk mailing list
360 (which is mirrored to comp.lang.ruby) or contact mailto:david@loudthinking.com.
Note: See TracBrowser for help on using the browser.