2004-11-23 20:04:44 -05:00
|
|
|
require 'yaml'
|
2005-11-02 12:30:59 -05:00
|
|
|
require 'set'
|
2009-10-09 08:52:25 -04:00
|
|
|
require 'active_support/benchmarkable'
|
2009-04-22 19:41:06 -04:00
|
|
|
require 'active_support/dependencies'
|
2010-06-19 11:15:21 -04:00
|
|
|
require 'active_support/descendants_tracker'
|
2009-05-20 20:11:41 -04:00
|
|
|
require 'active_support/time'
|
2010-04-10 22:30:50 -04:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
2009-05-13 04:10:37 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
|
|
|
require 'active_support/core_ext/class/delegating_attributes'
|
|
|
|
require 'active_support/core_ext/array/extract_options'
|
|
|
|
require 'active_support/core_ext/hash/deep_merge'
|
|
|
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
|
|
require 'active_support/core_ext/hash/slice'
|
|
|
|
require 'active_support/core_ext/string/behavior'
|
2010-04-05 15:15:08 -04:00
|
|
|
require 'active_support/core_ext/kernel/singleton_class'
|
2009-12-25 16:23:10 -05:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2010-06-14 12:36:04 -04:00
|
|
|
require 'active_support/core_ext/module/introspection'
|
2010-03-28 07:26:13 -04:00
|
|
|
require 'active_support/core_ext/object/duplicable'
|
2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2011-11-29 07:28:04 -05:00
|
|
|
require 'active_support/deprecation'
|
2010-03-07 09:24:30 -05:00
|
|
|
require 'arel'
|
|
|
|
require 'active_record/errors'
|
2010-06-24 07:23:43 -04:00
|
|
|
require 'active_record/log_subscriber'
|
2011-12-04 14:29:56 -05:00
|
|
|
require 'active_record/explain_subscriber'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
module ActiveRecord #:nodoc:
|
2010-01-29 20:02:12 -05:00
|
|
|
# = Active Record
|
2010-06-15 14:11:41 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Active Record objects don't specify their attributes directly, but rather infer them from
|
|
|
|
# the table definition with which they're linked. Adding, removing, and changing attributes
|
|
|
|
# and their type is done directly in the database. Any change is instantly reflected in the
|
2010-08-02 11:09:31 -04:00
|
|
|
# Active Record objects. The mapping that binds a given Active Record class to a certain
|
2005-07-03 04:32:43 -04:00
|
|
|
# database table will happen automatically in most common cases, but can be overwritten for the uncommon ones.
|
|
|
|
#
|
2010-08-24 06:22:54 -04:00
|
|
|
# See the mapping rules in table_name and the full example in link:files/activerecord/README_rdoc.html for more insight.
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Creation
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Active Records accept constructor parameters either in a hash or as a block. The hash
|
|
|
|
# method is especially useful when you're receiving the data from somewhere else, like an
|
2010-08-02 11:09:31 -04:00
|
|
|
# HTTP request. It works like this:
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2005-04-17 13:16:24 -04:00
|
|
|
# user = User.new(:name => "David", :occupation => "Code Artist")
|
2004-11-23 20:04:44 -05:00
|
|
|
# user.name # => "David"
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# You can also use block initialization:
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# user = User.new do |u|
|
|
|
|
# u.name = "David"
|
|
|
|
# u.occupation = "Code Artist"
|
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# And of course you can just create a bare object and specify the attributes after the fact:
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# user = User.new
|
|
|
|
# user.name = "David"
|
|
|
|
# user.occupation = "Code Artist"
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Conditions
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2006-06-03 18:15:06 -04:00
|
|
|
# Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement.
|
2011-08-04 18:14:06 -04:00
|
|
|
# The array form is to be used when the condition input is tainted and requires sanitization. The string form can
|
|
|
|
# be used for statements that don't involve tainted data. The hash form works much like the array form, except
|
|
|
|
# only equality and range is possible. Examples:
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2006-06-02 20:01:08 -04:00
|
|
|
# class User < ActiveRecord::Base
|
2004-11-23 20:04:44 -05:00
|
|
|
# def self.authenticate_unsafely(user_name, password)
|
2010-04-08 11:49:38 -04:00
|
|
|
# where("user_name = '#{user_name}' AND password = '#{password}'").first
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# def self.authenticate_safely(user_name, password)
|
2010-04-08 11:49:38 -04:00
|
|
|
# where("user_name = ? AND password = ?", user_name, password).first
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
2006-06-03 18:15:06 -04:00
|
|
|
#
|
|
|
|
# def self.authenticate_safely_simply(user_name, password)
|
2010-04-08 11:49:38 -04:00
|
|
|
# where(:user_name => user_name, :password => password).first
|
2006-06-03 18:15:06 -04:00
|
|
|
# end
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# The <tt>authenticate_unsafely</tt> method inserts the parameters directly into the query
|
|
|
|
# and is thus susceptible to SQL-injection attacks if the <tt>user_name</tt> and +password+
|
2011-05-02 09:37:34 -04:00
|
|
|
# parameters come directly from an HTTP request. The <tt>authenticate_safely</tt> and
|
2010-08-14 01:13:00 -04:00
|
|
|
# <tt>authenticate_safely_simply</tt> both will sanitize the <tt>user_name</tt> and +password+
|
|
|
|
# before inserting them in the query, which will ensure that an attacker can't escape the
|
2010-08-02 11:09:31 -04:00
|
|
|
# query and fake the login (or worse).
|
2004-12-06 13:08:35 -05:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# When using multiple parameters in the conditions, it can easily become hard to read exactly
|
|
|
|
# what the fourth or fifth question mark is supposed to represent. In those cases, you can
|
|
|
|
# resort to named bind variables instead. That's done by replacing the question marks with
|
2010-08-02 11:09:31 -04:00
|
|
|
# symbols and supplying a hash with values for the matching symbol keys:
|
2005-03-27 07:04:07 -05:00
|
|
|
#
|
2010-04-08 11:49:38 -04:00
|
|
|
# Company.where(
|
2005-07-03 04:32:43 -04:00
|
|
|
# "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
|
2005-03-27 07:04:07 -05:00
|
|
|
# { :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' }
|
2010-04-08 11:49:38 -04:00
|
|
|
# ).first
|
2005-03-27 07:04:07 -05:00
|
|
|
#
|
2006-06-03 18:15:06 -04:00
|
|
|
# Similarly, a simple hash without a statement will generate conditions based on equality with the SQL AND
|
|
|
|
# operator. For instance:
|
|
|
|
#
|
2010-04-08 11:49:38 -04:00
|
|
|
# Student.where(:first_name => "Harvey", :status => 1)
|
|
|
|
# Student.where(params[:student])
|
2006-06-03 18:15:06 -04:00
|
|
|
#
|
2007-01-10 05:13:18 -05:00
|
|
|
# A range may be used in the hash to use the SQL BETWEEN operator:
|
|
|
|
#
|
2010-04-08 11:49:38 -04:00
|
|
|
# Student.where(:grade => 9..12)
|
2006-06-03 18:15:06 -04:00
|
|
|
#
|
2008-04-04 23:52:58 -04:00
|
|
|
# An array may be used in the hash to use the SQL IN operator:
|
|
|
|
#
|
2010-04-08 11:49:38 -04:00
|
|
|
# Student.where(:grade => [9,11,12])
|
2008-04-04 23:52:58 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# When joining tables, nested hashes or keys written in the form 'table_name.column_name'
|
2010-08-02 11:09:31 -04:00
|
|
|
# can be used to qualify the table name of a particular condition. For instance:
|
2009-07-25 11:03:58 -04:00
|
|
|
#
|
2011-10-12 13:46:08 -04:00
|
|
|
# Student.joins(:schools).where(:schools => { :category => 'public' })
|
|
|
|
# Student.joins(:schools).where('schools.category' => 'public' )
|
2009-07-25 11:03:58 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Overwriting default accessors
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# All column values are automatically available through basic accessors on the Active Record
|
|
|
|
# object, but sometimes you want to specialize this behavior. This can be done by overwriting
|
|
|
|
# the default accessors (using the same name as the attribute) and calling
|
|
|
|
# <tt>read_attribute(attr_name)</tt> and <tt>write_attribute(attr_name, value)</tt> to actually
|
2010-08-02 11:09:31 -04:00
|
|
|
# change things.
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# class Song < ActiveRecord::Base
|
|
|
|
# # Uses an integer of seconds to hold the length of the song
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# def length=(minutes)
|
2008-05-02 09:45:23 -04:00
|
|
|
# write_attribute(:length, minutes.to_i * 60)
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# def length
|
2005-04-17 13:16:24 -04:00
|
|
|
# read_attribute(:length) / 60
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# You can alternatively use <tt>self[:attribute]=(value)</tt> and <tt>self[:attribute]</tt>
|
2010-08-02 11:09:31 -04:00
|
|
|
# instead of <tt>write_attribute(:attribute, value)</tt> and <tt>read_attribute(:attribute)</tt>.
|
2005-04-17 13:16:24 -04:00
|
|
|
#
|
2007-12-05 09:22:26 -05:00
|
|
|
# == Attribute query methods
|
|
|
|
#
|
|
|
|
# In addition to the basic accessors, query methods are also automatically available on the Active Record object.
|
|
|
|
# Query methods allow you to test whether an attribute value is present.
|
2007-12-09 23:13:33 -05:00
|
|
|
#
|
2007-12-05 09:22:26 -05:00
|
|
|
# For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call
|
|
|
|
# to determine whether the user has a name:
|
|
|
|
#
|
|
|
|
# user = User.new(:name => "David")
|
|
|
|
# user.name? # => true
|
|
|
|
#
|
|
|
|
# anonymous = User.new(:name => "")
|
|
|
|
# anonymous.name? # => false
|
|
|
|
#
|
2005-10-10 23:55:49 -04:00
|
|
|
# == Accessing attributes before they have been typecasted
|
2005-02-23 18:51:34 -05:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Sometimes you want to be able to read the raw attribute data without having the column-determined
|
|
|
|
# typecast run its course first. That can be done by using the <tt><attribute>_before_type_cast</tt>
|
|
|
|
# accessors that all attributes have. For example, if your Account model has a <tt>balance</tt> attribute,
|
2010-08-02 11:09:31 -04:00
|
|
|
# you can call <tt>account.balance_before_type_cast</tt> or <tt>account.id_before_type_cast</tt>.
|
2005-02-23 18:51:34 -05:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# This is especially useful in validation situations where the user might supply a string for an
|
|
|
|
# integer field and you want to display the original string back in an error message. Accessing the
|
2010-08-02 11:09:31 -04:00
|
|
|
# attribute normally would typecast the string to 0, which isn't what you want.
|
2005-02-23 18:51:34 -05:00
|
|
|
#
|
2005-01-02 08:31:00 -05:00
|
|
|
# == Dynamic attribute-based finders
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects
|
|
|
|
# by simple queries without turning to SQL. They work by appending the name of an attribute
|
|
|
|
# to <tt>find_by_</tt>, <tt>find_last_by_</tt>, or <tt>find_all_by_</tt> and thus produces finders
|
|
|
|
# like <tt>Person.find_by_user_name</tt>, <tt>Person.find_all_by_last_name</tt>, and
|
|
|
|
# <tt>Payment.find_by_transaction_id</tt>. Instead of writing
|
2010-04-08 11:49:38 -04:00
|
|
|
# <tt>Person.where(:user_name => user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
|
2010-08-14 01:13:00 -04:00
|
|
|
# And instead of writing <tt>Person.where(:last_name => last_name).all</tt>, you just do
|
2010-08-02 11:09:31 -04:00
|
|
|
# <tt>Person.find_all_by_last_name(last_name)</tt>.
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2011-08-08 20:31:03 -04:00
|
|
|
# It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
|
2011-08-16 10:01:18 -04:00
|
|
|
# <tt>ActiveRecord::RecordNotFound</tt> error if they do not return any records,
|
2011-08-08 20:31:03 -04:00
|
|
|
# like <tt>Person.find_by_last_name!</tt>.
|
|
|
|
#
|
2010-08-02 13:07:18 -04:00
|
|
|
# It's also possible to use multiple attributes in the same find by separating them with "_and_".
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-08-02 13:07:18 -04:00
|
|
|
# Person.where(:user_name => user_name, :password => password).first
|
2011-02-18 16:54:02 -05:00
|
|
|
# Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-02 13:07:18 -04:00
|
|
|
# It's even possible to call these dynamic finder methods on relations and named scopes.
|
2010-04-08 11:49:38 -04:00
|
|
|
#
|
|
|
|
# Payment.order("created_on").find_all_by_amount(50)
|
|
|
|
# Payment.pending.find_last_by_amount(100)
|
2005-01-02 08:51:00 -05:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# The same dynamic finder style can be used to create the object if it doesn't already exist.
|
|
|
|
# This dynamic finder is called with <tt>find_or_create_by_</tt> and will return the object if
|
|
|
|
# it already exists and otherwise creates it, then returns it. Protected attributes won't be set
|
|
|
|
# unless they are given in a block.
|
2005-11-04 14:39:50 -05:00
|
|
|
#
|
|
|
|
# # No 'Summer' tag exists
|
|
|
|
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
|
2007-12-09 23:13:33 -05:00
|
|
|
#
|
2005-11-04 14:39:50 -05:00
|
|
|
# # Now the 'Summer' tag does exist
|
|
|
|
# Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
|
|
|
|
#
|
2008-03-25 19:56:48 -04:00
|
|
|
# # Now 'Bob' exist and is an 'admin'
|
|
|
|
# User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true }
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without
|
2010-08-02 11:09:31 -04:00
|
|
|
# saving it first. Protected attributes won't be set unless they are given in a block.
|
2006-06-20 18:48:52 -04:00
|
|
|
#
|
|
|
|
# # No 'Winter' tag exists
|
|
|
|
# winter = Tag.find_or_initialize_by_name("Winter")
|
2010-11-07 09:05:18 -05:00
|
|
|
# winter.persisted? # false
|
2006-06-20 18:48:52 -04:00
|
|
|
#
|
2007-03-13 21:08:45 -04:00
|
|
|
# To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
|
2010-08-02 11:09:31 -04:00
|
|
|
# a list of parameters.
|
2007-03-13 21:08:45 -04:00
|
|
|
#
|
|
|
|
# Tag.find_or_create_by_name(:name => "rails", :creator => current_user)
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# That will either find an existing tag named "rails", or create a new one while setting the
|
2010-08-02 11:09:31 -04:00
|
|
|
# user that created it.
|
2007-03-13 21:08:45 -04:00
|
|
|
#
|
2010-08-02 13:07:18 -04:00
|
|
|
# Just like <tt>find_by_*</tt>, you can also use <tt>scoped_by_*</tt> to retrieve data. The good thing about
|
|
|
|
# using this feature is that the very first time result is returned using <tt>method_missing</tt> technique
|
|
|
|
# but after that the method is declared on the class. Henceforth <tt>method_missing</tt> will not be hit.
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# User.scoped_by_user_name('David')
|
2010-08-02 13:07:18 -04:00
|
|
|
#
|
2005-02-07 09:15:53 -05:00
|
|
|
# == Saving arrays, hashes, and other non-mappable objects in text columns
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Active Record can serialize any object in text columns using YAML. To do so, you must
|
2010-08-02 11:09:31 -04:00
|
|
|
# specify this with a call to the class method +serialize+.
|
2010-08-14 01:13:00 -04:00
|
|
|
# This makes it possible to store arrays, hashes, and other non-mappable objects without doing
|
|
|
|
# any additional work.
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# serialize :preferences
|
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2006-04-26 17:29:10 -04:00
|
|
|
# user = User.create(:preferences => { "background" => "black", "display" => large })
|
2004-11-23 20:04:44 -05:00
|
|
|
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# You can also specify a class option as the second parameter that'll raise an exception
|
2010-08-02 11:09:31 -04:00
|
|
|
# if a serialized object is retrieved as a descendant of a class not in the hierarchy.
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# class User < ActiveRecord::Base
|
2005-01-25 15:00:20 -05:00
|
|
|
# serialize :preferences, Hash
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2005-04-17 13:16:24 -04:00
|
|
|
# user = User.create(:preferences => %w( one two three ))
|
2004-11-23 20:04:44 -05:00
|
|
|
# User.find(user.id).preferences # raises SerializationTypeMismatch
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2011-02-02 18:26:39 -05:00
|
|
|
# When you specify a class option, the default value for that attribute will be a new
|
|
|
|
# instance of that class.
|
|
|
|
#
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# serialize :preferences, OpenStruct
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# user = User.new
|
|
|
|
# user.preferences.theme_color = "red"
|
|
|
|
#
|
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Single table inheritance
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Active Record allows inheritance by storing the name of the class in a column that by
|
|
|
|
# default is named "type" (can be changed by overwriting <tt>Base.inheritance_column</tt>).
|
2010-08-02 11:09:31 -04:00
|
|
|
# This means that an inheritance looking like this:
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# class Company < ActiveRecord::Base; end
|
|
|
|
# class Firm < Company; end
|
|
|
|
# class Client < Company; end
|
|
|
|
# class PriorityClient < Client; end
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# When you do <tt>Firm.create(:name => "37signals")</tt>, this record will be saved in
|
|
|
|
# the companies table with type = "Firm". You can then fetch this row again using
|
2010-08-02 11:09:31 -04:00
|
|
|
# <tt>Company.where(:name => '37signals').first</tt> and it will return a Firm object.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# If you don't have a type column defined in your table, single-table inheritance won't
|
|
|
|
# be triggered. In that case, it'll work just like normal subclasses with no special magic
|
2010-08-02 11:09:31 -04:00
|
|
|
# for differentiating between them or reloading the right type with find.
|
2004-12-16 11:56:30 -05:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# Note, all the attributes for all the cases are kept in the same table. Read more:
|
|
|
|
# http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Connection to multiple databases in different models
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Connections are usually created through ActiveRecord::Base.establish_connection and retrieved
|
|
|
|
# by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this
|
|
|
|
# connection. But you can also set a class-specific connection. For example, if Course is an
|
2010-08-02 11:09:31 -04:00
|
|
|
# ActiveRecord::Base, but resides in a different database, you can just say <tt>Course.establish_connection</tt>
|
2008-05-25 07:29:00 -04:00
|
|
|
# and Course and all of its subclasses will use this connection instead.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# This feature is implemented by keeping a connection pool in ActiveRecord::Base that is
|
|
|
|
# a Hash indexed by the class. If a connection is requested, the retrieve_connection method
|
2010-08-02 11:09:31 -04:00
|
|
|
# will go up the class-hierarchy until a connection is found in the connection pool.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# == Exceptions
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2008-05-09 05:38:02 -04:00
|
|
|
# * ActiveRecordError - Generic error class and superclass of all other errors raised by Active Record.
|
|
|
|
# * AdapterNotSpecified - The configuration hash used in <tt>establish_connection</tt> didn't include an
|
2004-11-23 20:04:44 -05:00
|
|
|
# <tt>:adapter</tt> key.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * AdapterNotFound - The <tt>:adapter</tt> key used in <tt>establish_connection</tt> specified a
|
2010-08-02 11:09:31 -04:00
|
|
|
# non-existent adapter
|
2005-07-03 04:32:43 -04:00
|
|
|
# (or a bad spelling of an existing one).
|
2010-08-14 01:13:00 -04:00
|
|
|
# * AssociationTypeMismatch - The object assigned to the association wasn't of the type
|
2010-08-02 11:09:31 -04:00
|
|
|
# specified in the association definition.
|
2008-05-09 05:38:02 -04:00
|
|
|
# * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * ConnectionNotEstablished+ - No connection has been established. Use <tt>establish_connection</tt>
|
2010-08-02 11:09:31 -04:00
|
|
|
# before querying.
|
2008-05-09 05:38:02 -04:00
|
|
|
# * RecordNotFound - No record responded to the +find+ method. Either the row with the given ID doesn't exist
|
|
|
|
# or the row didn't meet the additional restrictions. Some +find+ calls do not raise this exception to signal
|
|
|
|
# nothing was found, please check its documentation for further details.
|
|
|
|
# * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
|
|
|
|
# * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
|
2010-08-14 01:13:00 -04:00
|
|
|
# <tt>attributes=</tt> method. The +errors+ property of this exception contains an array of
|
2010-08-02 11:09:31 -04:00
|
|
|
# AttributeAssignmentError
|
2005-03-06 09:11:26 -05:00
|
|
|
# objects that should be inspected to determine which attributes triggered the errors.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * AttributeAssignmentError - An error occurred while doing a mass assignment through the
|
2010-08-02 11:09:31 -04:00
|
|
|
# <tt>attributes=</tt> method.
|
2010-08-14 01:13:00 -04:00
|
|
|
# You can inspect the +attribute+ property of the exception object to determine which attribute
|
2010-08-02 11:09:31 -04:00
|
|
|
# triggered the error.
|
2005-09-11 05:41:24 -04:00
|
|
|
#
|
2005-07-03 04:32:43 -04:00
|
|
|
# *Note*: The attributes listed are class-level attributes (accessible from both the class and instance level).
|
2008-05-09 05:38:02 -04:00
|
|
|
# So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
|
2004-11-23 20:04:44 -05:00
|
|
|
# instances in the current object space.
|
|
|
|
class Base
|
2011-12-18 16:42:09 -05:00
|
|
|
include ActiveRecord::Model
|
2008-11-24 12:14:24 -05:00
|
|
|
end
|
2006-06-19 18:48:51 -04:00
|
|
|
end
|
2011-12-24 07:30:10 -05:00
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Model::DeprecationProxy)
|