2016-08-06 12:24:04 -04:00
|
|
|
require "yaml"
|
|
|
|
require "active_support/benchmarkable"
|
|
|
|
require "active_support/dependencies"
|
|
|
|
require "active_support/descendants_tracker"
|
|
|
|
require "active_support/time"
|
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
|
|
|
require "active_support/core_ext/array/extract_options"
|
|
|
|
require "active_support/core_ext/hash/deep_merge"
|
|
|
|
require "active_support/core_ext/hash/slice"
|
|
|
|
require "active_support/core_ext/hash/transform_values"
|
|
|
|
require "active_support/core_ext/string/behavior"
|
|
|
|
require "active_support/core_ext/kernel/singleton_class"
|
|
|
|
require "active_support/core_ext/module/introspection"
|
|
|
|
require "active_support/core_ext/object/duplicable"
|
|
|
|
require "active_support/core_ext/class/subclasses"
|
|
|
|
require "active_record/attribute_decorators"
|
Deprecate the behavior of AR::Dirty inside of after_(create|update|save) callbacks
We pretty frequently get bug reports that "dirty is broken inside of
after callbacks". Intuitively they are correct. You'd expect
`Model.after_save { puts changed? }; model.save` to do the same thing as
`model.save; puts model.changed?`, but it does not.
However, changing this goes much farther than just making the behavior
more intuitive. There are a _ton_ of places inside of AR that can be
drastically simplified with this change. Specifically, autosave
associations, timestamps, touch, counter cache, and just about anything
else in AR that works with callbacks have code to try to avoid "double
save" bugs which we will be able to flat out remove with this change.
We introduce two new sets of methods, both with names that are meant to
be more explicit than dirty. The first set maintains the old behavior,
and their names are meant to center that they are about changes that
occurred during the save that just happened. They are equivalent to
`previous_changes` when called outside of after callbacks, or once the
deprecation cycle moves.
The second set is the new behavior. Their names imply that they are
talking about changes from the database representation. The fact that
this is what we really care about became clear when looking at
`BelongsTo.touch_record` when tests were failing. I'm unsure that this
set of methods should be in the public API. Outside of after callbacks,
they are equivalent to the existing methods on dirty.
Dirty itself is not deprecated, nor are the methods inside of it. They
will only emit the warning when called inside of after callbacks. The
scope of this breakage is pretty large, but the migration path is
simple. Given how much this can improve our codebase, and considering
that it makes our API more intuitive, I think it's worth doing.
2016-06-09 10:07:12 -04:00
|
|
|
require "active_record/define_callbacks"
|
2016-08-06 12:24:04 -04:00
|
|
|
require "active_record/errors"
|
|
|
|
require "active_record/log_subscriber"
|
|
|
|
require "active_record/explain_subscriber"
|
|
|
|
require "active_record/relation/delegation"
|
|
|
|
require "active_record/attributes"
|
|
|
|
require "active_record/type_caster"
|
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
|
|
|
#
|
2012-11-08 16:16:54 -05: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)
|
2012-11-08 16:16:54 -05: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",
|
2012-11-08 16:16:54 -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:
|
|
|
|
#
|
2012-11-08 16:16:54 -05:00
|
|
|
# Student.where(first_name: "Harvey", status: 1)
|
2010-04-08 11:49:38 -04:00
|
|
|
# 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:
|
|
|
|
#
|
2012-11-08 16:16:54 -05: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:
|
|
|
|
#
|
2012-11-08 16:16:54 -05: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
|
|
|
#
|
2012-11-08 16:16:54 -05:00
|
|
|
# Student.joins(:schools).where(schools: { category: 'public' })
|
2011-10-12 13:46:08 -04:00
|
|
|
# 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
|
2015-04-18 07:01:16 -04:00
|
|
|
# +super+ to actually 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)
|
2015-04-16 19:46:03 -04:00
|
|
|
# super(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
|
2015-04-16 19:46:03 -04:00
|
|
|
# super / 60
|
2004-11-23 20:04:44 -05:00
|
|
|
# end
|
|
|
|
# end
|
2005-07-03 04:32:43 -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.
|
2014-12-23 19:32:49 -05:00
|
|
|
# Query methods allow you to test whether an attribute value is present.
|
|
|
|
# Additionally, when dealing with numeric values, a query method will return false if the value is zero.
|
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:
|
|
|
|
#
|
2012-11-08 16:16:54 -05:00
|
|
|
# user = User.new(name: "David")
|
2007-12-05 09:22:26 -05:00
|
|
|
# user.name? # => true
|
|
|
|
#
|
2012-11-08 16:16:54 -05:00
|
|
|
# anonymous = User.new(name: "")
|
2007-12-05 09:22:26 -05:00
|
|
|
# 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
|
|
|
|
#
|
2013-04-02 15:09:15 -04:00
|
|
|
# Dynamic attribute-based finders are a mildly deprecated way of getting (and/or creating) objects
|
2010-08-14 01:13:00 -04:00
|
|
|
# by simple queries without turning to SQL. They work by appending the name of an attribute
|
2013-01-18 04:14:18 -05:00
|
|
|
# to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>.
|
2013-04-02 15:09:15 -04:00
|
|
|
# Instead of writing <tt>Person.find_by(user_name: user_name)</tt>, you can use
|
2013-01-01 14:42:47 -05:00
|
|
|
# <tt>Person.find_by_user_name(user_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
|
2015-07-08 06:16:16 -04:00
|
|
|
# ActiveRecord::RecordNotFound error if they do not return any records,
|
2011-08-08 20:31:03 -04:00
|
|
|
# like <tt>Person.find_by_last_name!</tt>.
|
|
|
|
#
|
2016-03-26 04:14:49 -04:00
|
|
|
# It's also possible to use multiple attributes in the same <tt>find_by_</tt> by separating them with
|
|
|
|
# "_and_".
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2013-04-02 15:09:15 -04:00
|
|
|
# Person.find_by(user_name: user_name, password: password)
|
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
|
|
|
#
|
2013-01-01 14:36:33 -05:00
|
|
|
# Payment.order("created_on").find_by_amount(50)
|
2005-01-02 08:51:00 -05: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
|
2015-07-08 06:16:16 -04:00
|
|
|
# specify this with a call to the class method
|
|
|
|
# {serialize}[rdoc-ref:AttributeMethods::Serialization::ClassMethods#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
|
|
|
#
|
2012-11-08 16:16:54 -05: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
|
|
|
#
|
2012-11-08 16:16:54 -05: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
|
|
|
|
#
|
2014-06-30 11:29:32 -04:00
|
|
|
# Active Record allows inheritance by storing the name of the class in a
|
|
|
|
# column that is named "type" by default. See ActiveRecord::Inheritance for
|
|
|
|
# more details.
|
2005-07-03 04:32:43 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Connection to multiple databases in different models
|
|
|
|
#
|
2015-07-08 06:16:16 -04:00
|
|
|
# Connections are usually created through
|
|
|
|
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] and retrieved
|
2010-08-14 01:13:00 -04:00
|
|
|
# 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
|
2015-07-08 06:16:16 -04:00
|
|
|
# a hash indexed by the class. If a connection is requested, the
|
|
|
|
# {ActiveRecord::Base.retrieve_connection}[rdoc-ref:ConnectionHandling#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.
|
2015-07-08 06:16:16 -04:00
|
|
|
# * AdapterNotSpecified - The configuration hash used in
|
|
|
|
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection]
|
|
|
|
# didn't include an <tt>:adapter</tt> key.
|
|
|
|
# * AdapterNotFound - The <tt>:adapter</tt> key used in
|
|
|
|
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection]
|
|
|
|
# specified a 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.
|
2012-04-30 08:46:30 -04:00
|
|
|
# * AttributeAssignmentError - An error occurred while doing a mass assignment through the
|
2015-07-08 06:16:16 -04:00
|
|
|
# {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
|
2012-04-30 08:46:30 -04:00
|
|
|
# You can inspect the +attribute+ property of the exception object to determine which attribute
|
|
|
|
# triggered the error.
|
2015-07-08 06:16:16 -04:00
|
|
|
# * ConnectionNotEstablished - No connection has been established.
|
|
|
|
# Use {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] before querying.
|
2008-05-09 05:38:02 -04:00
|
|
|
# * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
|
2015-07-08 06:16:16 -04:00
|
|
|
# {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] 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.
|
2015-12-16 23:44:21 -05:00
|
|
|
# * RecordInvalid - raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
|
2015-07-08 06:16:16 -04:00
|
|
|
# {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!]
|
|
|
|
# when the record is invalid.
|
|
|
|
# * RecordNotFound - No record responded to the {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] method.
|
|
|
|
# Either the row with the given ID doesn't exist or the row didn't meet the additional restrictions.
|
|
|
|
# Some {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] calls do not raise this exception to signal
|
2012-04-30 08:46:30 -04:00
|
|
|
# nothing was found, please check its documentation for further details.
|
|
|
|
# * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
|
|
|
|
# * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
|
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
|
2012-10-26 10:51:02 -04:00
|
|
|
extend ActiveModel::Naming
|
|
|
|
|
|
|
|
extend ActiveSupport::Benchmarkable
|
|
|
|
extend ActiveSupport::DescendantsTracker
|
|
|
|
|
|
|
|
extend ConnectionHandling
|
|
|
|
extend QueryCache::ClassMethods
|
|
|
|
extend Querying
|
|
|
|
extend Translation
|
|
|
|
extend DynamicMatchers
|
|
|
|
extend Explain
|
2013-11-02 15:01:31 -04:00
|
|
|
extend Enum
|
2013-08-30 19:48:20 -04:00
|
|
|
extend Delegation::DelegateCache
|
2015-07-14 18:47:16 -04:00
|
|
|
extend CollectionCacheKey
|
2012-10-26 10:51:02 -04:00
|
|
|
|
2014-01-22 09:28:19 -05:00
|
|
|
include Core
|
2012-10-26 10:51:02 -04:00
|
|
|
include Persistence
|
|
|
|
include ReadonlyAttributes
|
|
|
|
include ModelSchema
|
|
|
|
include Inheritance
|
|
|
|
include Scoping
|
|
|
|
include Sanitization
|
|
|
|
include AttributeAssignment
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
include Integration
|
|
|
|
include Validations
|
|
|
|
include CounterCache
|
2014-06-16 16:55:01 -04:00
|
|
|
include Attributes
|
|
|
|
include AttributeDecorators
|
2014-06-17 11:41:06 -04:00
|
|
|
include Locking::Optimistic
|
|
|
|
include Locking::Pessimistic
|
Deprecate the behavior of AR::Dirty inside of after_(create|update|save) callbacks
We pretty frequently get bug reports that "dirty is broken inside of
after callbacks". Intuitively they are correct. You'd expect
`Model.after_save { puts changed? }; model.save` to do the same thing as
`model.save; puts model.changed?`, but it does not.
However, changing this goes much farther than just making the behavior
more intuitive. There are a _ton_ of places inside of AR that can be
drastically simplified with this change. Specifically, autosave
associations, timestamps, touch, counter cache, and just about anything
else in AR that works with callbacks have code to try to avoid "double
save" bugs which we will be able to flat out remove with this change.
We introduce two new sets of methods, both with names that are meant to
be more explicit than dirty. The first set maintains the old behavior,
and their names are meant to center that they are about changes that
occurred during the save that just happened. They are equivalent to
`previous_changes` when called outside of after callbacks, or once the
deprecation cycle moves.
The second set is the new behavior. Their names imply that they are
talking about changes from the database representation. The fact that
this is what we really care about became clear when looking at
`BelongsTo.touch_record` when tests were failing. I'm unsure that this
set of methods should be in the public API. Outside of after callbacks,
they are equivalent to the existing methods on dirty.
Dirty itself is not deprecated, nor are the methods inside of it. They
will only emit the warning when called inside of after callbacks. The
scope of this breakage is pretty large, but the migration path is
simple. Given how much this can improve our codebase, and considering
that it makes our API more intuitive, I think it's worth doing.
2016-06-09 10:07:12 -04:00
|
|
|
include DefineCallbacks
|
2012-10-26 10:51:02 -04:00
|
|
|
include AttributeMethods
|
2014-03-27 17:10:17 -04:00
|
|
|
include Callbacks
|
2014-06-09 16:25:20 -04:00
|
|
|
include Timestamp
|
2012-10-26 10:51:02 -04:00
|
|
|
include Associations
|
|
|
|
include ActiveModel::SecurePassword
|
|
|
|
include AutosaveAssociation
|
|
|
|
include NestedAttributes
|
|
|
|
include Aggregations
|
|
|
|
include Transactions
|
2015-03-13 13:14:55 -04:00
|
|
|
include TouchLater
|
2016-08-16 09:20:50 -04:00
|
|
|
include NoTouching
|
2012-10-26 10:51:02 -04:00
|
|
|
include Reflection
|
|
|
|
include Serialization
|
|
|
|
include Store
|
2014-12-28 17:24:10 -05:00
|
|
|
include SecureToken
|
2015-02-18 17:55:48 -05:00
|
|
|
include Suppressor
|
2008-11-24 12:14:24 -05:00
|
|
|
end
|
2012-10-20 14:35:16 -04:00
|
|
|
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveSupport.run_load_hooks(:active_record, Base)
|
|
|
|
end
|