activerecord 3.2.19 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1715 -604
- data/MIT-LICENSE +2 -2
- data/README.rdoc +40 -45
- data/examples/performance.rb +33 -22
- data/examples/simple.rb +3 -4
- data/lib/active_record/aggregations.rb +76 -51
- data/lib/active_record/association_relation.rb +35 -0
- data/lib/active_record/associations/alias_tracker.rb +54 -40
- data/lib/active_record/associations/association.rb +76 -56
- data/lib/active_record/associations/association_scope.rb +125 -93
- data/lib/active_record/associations/belongs_to_association.rb +57 -28
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -2
- data/lib/active_record/associations/builder/association.rb +120 -32
- data/lib/active_record/associations/builder/belongs_to.rb +115 -62
- data/lib/active_record/associations/builder/collection_association.rb +61 -53
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +117 -43
- data/lib/active_record/associations/builder/has_many.rb +9 -65
- data/lib/active_record/associations/builder/has_one.rb +18 -52
- data/lib/active_record/associations/builder/singular_association.rb +18 -19
- data/lib/active_record/associations/collection_association.rb +268 -186
- data/lib/active_record/associations/collection_proxy.rb +1003 -63
- data/lib/active_record/associations/foreign_association.rb +11 -0
- data/lib/active_record/associations/has_many_association.rb +81 -41
- data/lib/active_record/associations/has_many_through_association.rb +76 -55
- data/lib/active_record/associations/has_one_association.rb +51 -21
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +83 -108
- data/lib/active_record/associations/join_dependency/join_base.rb +7 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +30 -37
- data/lib/active_record/associations/join_dependency.rb +239 -155
- data/lib/active_record/associations/preloader/association.rb +97 -62
- data/lib/active_record/associations/preloader/collection_association.rb +2 -8
- data/lib/active_record/associations/preloader/has_many_through.rb +7 -3
- data/lib/active_record/associations/preloader/has_one.rb +0 -8
- data/lib/active_record/associations/preloader/singular_association.rb +3 -3
- data/lib/active_record/associations/preloader/through_association.rb +75 -33
- data/lib/active_record/associations/preloader.rb +111 -79
- data/lib/active_record/associations/singular_association.rb +35 -13
- data/lib/active_record/associations/through_association.rb +41 -19
- data/lib/active_record/associations.rb +727 -501
- data/lib/active_record/attribute/user_provided_default.rb +28 -0
- data/lib/active_record/attribute.rb +213 -0
- data/lib/active_record/attribute_assignment.rb +32 -162
- data/lib/active_record/attribute_decorators.rb +67 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +52 -7
- data/lib/active_record/attribute_methods/dirty.rb +101 -61
- data/lib/active_record/attribute_methods/primary_key.rb +50 -36
- data/lib/active_record/attribute_methods/query.rb +7 -6
- data/lib/active_record/attribute_methods/read.rb +56 -117
- data/lib/active_record/attribute_methods/serialization.rb +43 -96
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +93 -42
- data/lib/active_record/attribute_methods/write.rb +34 -45
- data/lib/active_record/attribute_methods.rb +333 -144
- data/lib/active_record/attribute_mutation_tracker.rb +70 -0
- data/lib/active_record/attribute_set/builder.rb +108 -0
- data/lib/active_record/attribute_set.rb +108 -0
- data/lib/active_record/attributes.rb +265 -0
- data/lib/active_record/autosave_association.rb +285 -223
- data/lib/active_record/base.rb +95 -490
- data/lib/active_record/callbacks.rb +95 -61
- data/lib/active_record/coders/json.rb +13 -0
- data/lib/active_record/coders/yaml_column.rb +28 -19
- data/lib/active_record/collection_cache_key.rb +40 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +724 -277
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +199 -192
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -26
- data/lib/active_record/connection_adapters/abstract/quoting.rb +140 -57
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +147 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +419 -276
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +105 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +963 -276
- data/lib/active_record/connection_adapters/abstract/transaction.rb +232 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +397 -106
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +643 -342
- data/lib/active_record/connection_adapters/column.rb +30 -259
- data/lib/active_record/connection_adapters/connection_specification.rb +263 -0
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +22 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +50 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +125 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +70 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +51 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +67 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +93 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +54 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +32 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +47 -196
- data/lib/active_record/connection_adapters/postgresql/column.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +170 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +42 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +70 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +48 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +10 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +39 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +93 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +31 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +116 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +49 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +180 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +47 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +682 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +558 -1039
- data/lib/active_record/connection_adapters/schema_cache.rb +74 -36
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +32 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +48 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +538 -24
- data/lib/active_record/connection_adapters/statement_pool.rb +31 -12
- data/lib/active_record/connection_handling.rb +155 -0
- data/lib/active_record/core.rb +561 -0
- data/lib/active_record/counter_cache.rb +146 -105
- data/lib/active_record/dynamic_matchers.rb +101 -64
- data/lib/active_record/enum.rb +234 -0
- data/lib/active_record/errors.rb +153 -56
- data/lib/active_record/explain.rb +15 -63
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +10 -6
- data/lib/active_record/fixture_set/file.rb +77 -0
- data/lib/active_record/fixtures.rb +355 -232
- data/lib/active_record/gem_version.rb +15 -0
- data/lib/active_record/inheritance.rb +144 -79
- data/lib/active_record/integration.rb +66 -13
- data/lib/active_record/internal_metadata.rb +56 -0
- data/lib/active_record/legacy_yaml_adapter.rb +46 -0
- data/lib/active_record/locale/en.yml +9 -1
- data/lib/active_record/locking/optimistic.rb +77 -56
- data/lib/active_record/locking/pessimistic.rb +6 -6
- data/lib/active_record/log_subscriber.rb +53 -28
- data/lib/active_record/migration/command_recorder.rb +166 -33
- data/lib/active_record/migration/compatibility.rb +126 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +792 -264
- data/lib/active_record/model_schema.rb +192 -130
- data/lib/active_record/nested_attributes.rb +238 -145
- data/lib/active_record/no_touching.rb +52 -0
- data/lib/active_record/null_relation.rb +89 -0
- data/lib/active_record/persistence.rb +357 -157
- data/lib/active_record/query_cache.rb +22 -43
- data/lib/active_record/querying.rb +34 -23
- data/lib/active_record/railtie.rb +88 -48
- data/lib/active_record/railties/console_sandbox.rb +3 -4
- data/lib/active_record/railties/controller_runtime.rb +5 -4
- data/lib/active_record/railties/databases.rake +170 -422
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +2 -5
- data/lib/active_record/reflection.rb +715 -189
- data/lib/active_record/relation/batches/batch_enumerator.rb +67 -0
- data/lib/active_record/relation/batches.rb +203 -50
- data/lib/active_record/relation/calculations.rb +203 -194
- data/lib/active_record/relation/delegation.rb +103 -25
- data/lib/active_record/relation/finder_methods.rb +457 -261
- data/lib/active_record/relation/from_clause.rb +32 -0
- data/lib/active_record/relation/merger.rb +167 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +43 -0
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +88 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +17 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +17 -0
- data/lib/active_record/relation/predicate_builder/class_handler.rb +27 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +57 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +33 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
- data/lib/active_record/relation/predicate_builder.rb +153 -48
- data/lib/active_record/relation/query_attribute.rb +19 -0
- data/lib/active_record/relation/query_methods.rb +1019 -194
- data/lib/active_record/relation/record_fetch_warning.rb +49 -0
- data/lib/active_record/relation/spawn_methods.rb +46 -150
- data/lib/active_record/relation/where_clause.rb +174 -0
- data/lib/active_record/relation/where_clause_factory.rb +38 -0
- data/lib/active_record/relation.rb +450 -245
- data/lib/active_record/result.rb +104 -12
- data/lib/active_record/runtime_registry.rb +22 -0
- data/lib/active_record/sanitization.rb +120 -94
- data/lib/active_record/schema.rb +28 -18
- data/lib/active_record/schema_dumper.rb +141 -74
- data/lib/active_record/schema_migration.rb +50 -0
- data/lib/active_record/scoping/default.rb +64 -57
- data/lib/active_record/scoping/named.rb +93 -108
- data/lib/active_record/scoping.rb +73 -121
- data/lib/active_record/secure_token.rb +38 -0
- data/lib/active_record/serialization.rb +7 -5
- data/lib/active_record/statement_cache.rb +113 -0
- data/lib/active_record/store.rb +173 -15
- data/lib/active_record/suppressor.rb +58 -0
- data/lib/active_record/table_metadata.rb +68 -0
- data/lib/active_record/tasks/database_tasks.rb +313 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +151 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +110 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +59 -0
- data/lib/active_record/timestamp.rb +42 -24
- data/lib/active_record/touch_later.rb +58 -0
- data/lib/active_record/transactions.rb +233 -105
- data/lib/active_record/type/adapter_specific_registry.rb +130 -0
- data/lib/active_record/type/date.rb +7 -0
- data/lib/active_record/type/date_time.rb +7 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
- data/lib/active_record/type/internal/abstract_json.rb +29 -0
- data/lib/active_record/type/internal/timezone.rb +15 -0
- data/lib/active_record/type/serialized.rb +63 -0
- data/lib/active_record/type/time.rb +20 -0
- data/lib/active_record/type/type_map.rb +64 -0
- data/lib/active_record/type.rb +72 -0
- data/lib/active_record/type_caster/connection.rb +29 -0
- data/lib/active_record/type_caster/map.rb +19 -0
- data/lib/active_record/type_caster.rb +7 -0
- data/lib/active_record/validations/absence.rb +23 -0
- data/lib/active_record/validations/associated.rb +33 -18
- data/lib/active_record/validations/length.rb +24 -0
- data/lib/active_record/validations/presence.rb +66 -0
- data/lib/active_record/validations/uniqueness.rb +128 -68
- data/lib/active_record/validations.rb +48 -40
- data/lib/active_record/version.rb +5 -7
- data/lib/active_record.rb +71 -47
- data/lib/rails/generators/active_record/migration/migration_generator.rb +56 -8
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +24 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +28 -16
- data/lib/rails/generators/active_record/migration.rb +18 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +38 -16
- data/lib/rails/generators/active_record/model/templates/application_record.rb +5 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +7 -6
- data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
- data/lib/rails/generators/active_record.rb +3 -11
- metadata +188 -134
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -441
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/serializers/xml_serializer.rb +0 -203
- data/lib/active_record/session_store.rb +0 -360
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
data/lib/active_record/base.rb
CHANGED
@@ -1,34 +1,25 @@
|
|
1
|
-
begin
|
2
|
-
require 'psych'
|
3
|
-
rescue LoadError
|
4
|
-
end
|
5
|
-
|
6
1
|
require 'yaml'
|
7
|
-
require 'set'
|
8
|
-
require 'thread'
|
9
2
|
require 'active_support/benchmarkable'
|
10
3
|
require 'active_support/dependencies'
|
11
4
|
require 'active_support/descendants_tracker'
|
12
5
|
require 'active_support/time'
|
13
|
-
require 'active_support/core_ext/
|
14
|
-
require 'active_support/core_ext/class/attribute_accessors'
|
15
|
-
require 'active_support/core_ext/class/delegating_attributes'
|
16
|
-
require 'active_support/core_ext/class/attribute'
|
6
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
17
7
|
require 'active_support/core_ext/array/extract_options'
|
18
8
|
require 'active_support/core_ext/hash/deep_merge'
|
19
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
20
9
|
require 'active_support/core_ext/hash/slice'
|
10
|
+
require 'active_support/core_ext/hash/transform_values'
|
21
11
|
require 'active_support/core_ext/string/behavior'
|
22
12
|
require 'active_support/core_ext/kernel/singleton_class'
|
23
|
-
require 'active_support/core_ext/module/delegation'
|
24
13
|
require 'active_support/core_ext/module/introspection'
|
25
14
|
require 'active_support/core_ext/object/duplicable'
|
26
|
-
require 'active_support/core_ext/
|
27
|
-
require '
|
28
|
-
require 'arel'
|
15
|
+
require 'active_support/core_ext/class/subclasses'
|
16
|
+
require 'active_record/attribute_decorators'
|
29
17
|
require 'active_record/errors'
|
30
18
|
require 'active_record/log_subscriber'
|
31
19
|
require 'active_record/explain_subscriber'
|
20
|
+
require 'active_record/relation/delegation'
|
21
|
+
require 'active_record/attributes'
|
22
|
+
require 'active_record/type_caster'
|
32
23
|
|
33
24
|
module ActiveRecord #:nodoc:
|
34
25
|
# = Active Record
|
@@ -47,7 +38,7 @@ module ActiveRecord #:nodoc:
|
|
47
38
|
# method is especially useful when you're receiving the data from somewhere else, like an
|
48
39
|
# HTTP request. It works like this:
|
49
40
|
#
|
50
|
-
# user = User.new(:
|
41
|
+
# user = User.new(name: "David", occupation: "Code Artist")
|
51
42
|
# user.name # => "David"
|
52
43
|
#
|
53
44
|
# You can also use block initialization:
|
@@ -80,7 +71,7 @@ module ActiveRecord #:nodoc:
|
|
80
71
|
# end
|
81
72
|
#
|
82
73
|
# def self.authenticate_safely_simply(user_name, password)
|
83
|
-
# where(:
|
74
|
+
# where(user_name: user_name, password: password).first
|
84
75
|
# end
|
85
76
|
# end
|
86
77
|
#
|
@@ -98,27 +89,27 @@ module ActiveRecord #:nodoc:
|
|
98
89
|
#
|
99
90
|
# Company.where(
|
100
91
|
# "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
|
101
|
-
# { :
|
92
|
+
# { id: 3, name: "37signals", division: "First", accounting_date: '2005-01-01' }
|
102
93
|
# ).first
|
103
94
|
#
|
104
95
|
# Similarly, a simple hash without a statement will generate conditions based on equality with the SQL AND
|
105
96
|
# operator. For instance:
|
106
97
|
#
|
107
|
-
# Student.where(:
|
98
|
+
# Student.where(first_name: "Harvey", status: 1)
|
108
99
|
# Student.where(params[:student])
|
109
100
|
#
|
110
101
|
# A range may be used in the hash to use the SQL BETWEEN operator:
|
111
102
|
#
|
112
|
-
# Student.where(:
|
103
|
+
# Student.where(grade: 9..12)
|
113
104
|
#
|
114
105
|
# An array may be used in the hash to use the SQL IN operator:
|
115
106
|
#
|
116
|
-
# Student.where(:
|
107
|
+
# Student.where(grade: [9,11,12])
|
117
108
|
#
|
118
109
|
# When joining tables, nested hashes or keys written in the form 'table_name.column_name'
|
119
110
|
# can be used to qualify the table name of a particular condition. For instance:
|
120
111
|
#
|
121
|
-
# Student.joins(:schools).where(:
|
112
|
+
# Student.joins(:schools).where(schools: { category: 'public' })
|
122
113
|
# Student.joins(:schools).where('schools.category' => 'public' )
|
123
114
|
#
|
124
115
|
# == Overwriting default accessors
|
@@ -126,36 +117,33 @@ module ActiveRecord #:nodoc:
|
|
126
117
|
# All column values are automatically available through basic accessors on the Active Record
|
127
118
|
# object, but sometimes you want to specialize this behavior. This can be done by overwriting
|
128
119
|
# the default accessors (using the same name as the attribute) and calling
|
129
|
-
#
|
130
|
-
# change things.
|
120
|
+
# +super+ to actually change things.
|
131
121
|
#
|
132
122
|
# class Song < ActiveRecord::Base
|
133
123
|
# # Uses an integer of seconds to hold the length of the song
|
134
124
|
#
|
135
125
|
# def length=(minutes)
|
136
|
-
#
|
126
|
+
# super(minutes.to_i * 60)
|
137
127
|
# end
|
138
128
|
#
|
139
129
|
# def length
|
140
|
-
#
|
130
|
+
# super / 60
|
141
131
|
# end
|
142
132
|
# end
|
143
133
|
#
|
144
|
-
# You can alternatively use <tt>self[:attribute]=(value)</tt> and <tt>self[:attribute]</tt>
|
145
|
-
# instead of <tt>write_attribute(:attribute, value)</tt> and <tt>read_attribute(:attribute)</tt>.
|
146
|
-
#
|
147
134
|
# == Attribute query methods
|
148
135
|
#
|
149
136
|
# In addition to the basic accessors, query methods are also automatically available on the Active Record object.
|
150
137
|
# Query methods allow you to test whether an attribute value is present.
|
138
|
+
# Additionally, when dealing with numeric values, a query method will return false if the value is zero.
|
151
139
|
#
|
152
140
|
# For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call
|
153
141
|
# to determine whether the user has a name:
|
154
142
|
#
|
155
|
-
# user = User.new(:
|
143
|
+
# user = User.new(name: "David")
|
156
144
|
# user.name? # => true
|
157
145
|
#
|
158
|
-
# anonymous = User.new(:
|
146
|
+
# anonymous = User.new(name: "")
|
159
147
|
# anonymous.name? # => false
|
160
148
|
#
|
161
149
|
# == Accessing attributes before they have been typecasted
|
@@ -171,71 +159,31 @@ module ActiveRecord #:nodoc:
|
|
171
159
|
#
|
172
160
|
# == Dynamic attribute-based finders
|
173
161
|
#
|
174
|
-
# Dynamic attribute-based finders are a
|
162
|
+
# Dynamic attribute-based finders are a mildly deprecated way of getting (and/or creating) objects
|
175
163
|
# by simple queries without turning to SQL. They work by appending the name of an attribute
|
176
|
-
# to <tt>find_by_</tt
|
177
|
-
#
|
178
|
-
# <tt>
|
179
|
-
# <tt>Person.where(:user_name => user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
|
180
|
-
# And instead of writing <tt>Person.where(:last_name => last_name).all</tt>, you just do
|
181
|
-
# <tt>Person.find_all_by_last_name(last_name)</tt>.
|
164
|
+
# to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>.
|
165
|
+
# Instead of writing <tt>Person.find_by(user_name: user_name)</tt>, you can use
|
166
|
+
# <tt>Person.find_by_user_name(user_name)</tt>.
|
182
167
|
#
|
183
168
|
# It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
|
184
|
-
#
|
169
|
+
# ActiveRecord::RecordNotFound error if they do not return any records,
|
185
170
|
# like <tt>Person.find_by_last_name!</tt>.
|
186
171
|
#
|
187
|
-
# It's also possible to use multiple attributes in the same
|
172
|
+
# It's also possible to use multiple attributes in the same <tt>find_by_</tt> by separating them with
|
173
|
+
# "_and_".
|
188
174
|
#
|
189
|
-
# Person.
|
175
|
+
# Person.find_by(user_name: user_name, password: password)
|
190
176
|
# Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
|
191
177
|
#
|
192
178
|
# It's even possible to call these dynamic finder methods on relations and named scopes.
|
193
179
|
#
|
194
|
-
# Payment.order("created_on").
|
195
|
-
# Payment.pending.find_last_by_amount(100)
|
196
|
-
#
|
197
|
-
# The same dynamic finder style can be used to create the object if it doesn't already exist.
|
198
|
-
# This dynamic finder is called with <tt>find_or_create_by_</tt> and will return the object if
|
199
|
-
# it already exists and otherwise creates it, then returns it. Protected attributes won't be set
|
200
|
-
# unless they are given in a block.
|
201
|
-
#
|
202
|
-
# # No 'Summer' tag exists
|
203
|
-
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
|
204
|
-
#
|
205
|
-
# # Now the 'Summer' tag does exist
|
206
|
-
# Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
|
207
|
-
#
|
208
|
-
# # Now 'Bob' exist and is an 'admin'
|
209
|
-
# User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true }
|
210
|
-
#
|
211
|
-
# Adding an exclamation point (!) on to the end of <tt>find_or_create_by_</tt> will
|
212
|
-
# raise an <tt>ActiveRecord::RecordInvalid</tt> error if the new record is invalid.
|
213
|
-
#
|
214
|
-
# Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without
|
215
|
-
# saving it first. Protected attributes won't be set unless they are given in a block.
|
216
|
-
#
|
217
|
-
# # No 'Winter' tag exists
|
218
|
-
# winter = Tag.find_or_initialize_by_name("Winter")
|
219
|
-
# winter.persisted? # false
|
220
|
-
#
|
221
|
-
# To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
|
222
|
-
# a list of parameters.
|
223
|
-
#
|
224
|
-
# Tag.find_or_create_by_name(:name => "rails", :creator => current_user)
|
225
|
-
#
|
226
|
-
# That will either find an existing tag named "rails", or create a new one while setting the
|
227
|
-
# user that created it.
|
228
|
-
#
|
229
|
-
# Just like <tt>find_by_*</tt>, you can also use <tt>scoped_by_*</tt> to retrieve data. The good thing about
|
230
|
-
# using this feature is that the very first time result is returned using <tt>method_missing</tt> technique
|
231
|
-
# but after that the method is declared on the class. Henceforth <tt>method_missing</tt> will not be hit.
|
232
|
-
#
|
233
|
-
# User.scoped_by_user_name('David')
|
180
|
+
# Payment.order("created_on").find_by_amount(50)
|
234
181
|
#
|
235
182
|
# == Saving arrays, hashes, and other non-mappable objects in text columns
|
236
183
|
#
|
237
184
|
# Active Record can serialize any object in text columns using YAML. To do so, you must
|
238
|
-
# specify this with a call to the class method
|
185
|
+
# specify this with a call to the class method
|
186
|
+
# {serialize}[rdoc-ref:AttributeMethods::Serialization::ClassMethods#serialize].
|
239
187
|
# This makes it possible to store arrays, hashes, and other non-mappable objects without doing
|
240
188
|
# any additional work.
|
241
189
|
#
|
@@ -243,7 +191,7 @@ module ActiveRecord #:nodoc:
|
|
243
191
|
# serialize :preferences
|
244
192
|
# end
|
245
193
|
#
|
246
|
-
# user = User.create(:
|
194
|
+
# user = User.create(preferences: { "background" => "black", "display" => large })
|
247
195
|
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
|
248
196
|
#
|
249
197
|
# You can also specify a class option as the second parameter that'll raise an exception
|
@@ -253,7 +201,7 @@ module ActiveRecord #:nodoc:
|
|
253
201
|
# serialize :preferences, Hash
|
254
202
|
# end
|
255
203
|
#
|
256
|
-
# user = User.create(:
|
204
|
+
# user = User.create(preferences: %w( one two three ))
|
257
205
|
# User.find(user.id).preferences # raises SerializationTypeMismatch
|
258
206
|
#
|
259
207
|
# When you specify a class option, the default value for that attribute will be a new
|
@@ -269,452 +217,109 @@ module ActiveRecord #:nodoc:
|
|
269
217
|
#
|
270
218
|
# == Single table inheritance
|
271
219
|
#
|
272
|
-
# Active Record allows inheritance by storing the name of the class in a
|
273
|
-
#
|
274
|
-
#
|
275
|
-
#
|
276
|
-
# class Company < ActiveRecord::Base; end
|
277
|
-
# class Firm < Company; end
|
278
|
-
# class Client < Company; end
|
279
|
-
# class PriorityClient < Client; end
|
280
|
-
#
|
281
|
-
# When you do <tt>Firm.create(:name => "37signals")</tt>, this record will be saved in
|
282
|
-
# the companies table with type = "Firm". You can then fetch this row again using
|
283
|
-
# <tt>Company.where(:name => '37signals').first</tt> and it will return a Firm object.
|
284
|
-
#
|
285
|
-
# If you don't have a type column defined in your table, single-table inheritance won't
|
286
|
-
# be triggered. In that case, it'll work just like normal subclasses with no special magic
|
287
|
-
# for differentiating between them or reloading the right type with find.
|
288
|
-
#
|
289
|
-
# Note, all the attributes for all the cases are kept in the same table. Read more:
|
290
|
-
# http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html
|
220
|
+
# Active Record allows inheritance by storing the name of the class in a
|
221
|
+
# column that is named "type" by default. See ActiveRecord::Inheritance for
|
222
|
+
# more details.
|
291
223
|
#
|
292
224
|
# == Connection to multiple databases in different models
|
293
225
|
#
|
294
|
-
# Connections are usually created through
|
226
|
+
# Connections are usually created through
|
227
|
+
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] and retrieved
|
295
228
|
# by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this
|
296
229
|
# connection. But you can also set a class-specific connection. For example, if Course is an
|
297
230
|
# ActiveRecord::Base, but resides in a different database, you can just say <tt>Course.establish_connection</tt>
|
298
231
|
# and Course and all of its subclasses will use this connection instead.
|
299
232
|
#
|
300
233
|
# This feature is implemented by keeping a connection pool in ActiveRecord::Base that is
|
301
|
-
# a
|
234
|
+
# a hash indexed by the class. If a connection is requested, the
|
235
|
+
# {ActiveRecord::Base.retrieve_connection}[rdoc-ref:ConnectionHandling#retrieve_connection] method
|
302
236
|
# will go up the class-hierarchy until a connection is found in the connection pool.
|
303
237
|
#
|
304
238
|
# == Exceptions
|
305
239
|
#
|
306
240
|
# * ActiveRecordError - Generic error class and superclass of all other errors raised by Active Record.
|
307
|
-
# * AdapterNotSpecified - The configuration hash used in
|
308
|
-
#
|
309
|
-
#
|
310
|
-
#
|
241
|
+
# * AdapterNotSpecified - The configuration hash used in
|
242
|
+
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection]
|
243
|
+
# didn't include an <tt>:adapter</tt> key.
|
244
|
+
# * AdapterNotFound - The <tt>:adapter</tt> key used in
|
245
|
+
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection]
|
246
|
+
# specified a non-existent adapter
|
311
247
|
# (or a bad spelling of an existing one).
|
312
248
|
# * AssociationTypeMismatch - The object assigned to the association wasn't of the type
|
313
249
|
# specified in the association definition.
|
314
|
-
# * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
|
315
|
-
# * ConnectionNotEstablished+ - No connection has been established. Use <tt>establish_connection</tt>
|
316
|
-
# before querying.
|
317
|
-
# * RecordNotFound - No record responded to the +find+ method. Either the row with the given ID doesn't exist
|
318
|
-
# or the row didn't meet the additional restrictions. Some +find+ calls do not raise this exception to signal
|
319
|
-
# nothing was found, please check its documentation for further details.
|
320
|
-
# * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
|
321
|
-
# * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
|
322
|
-
# <tt>attributes=</tt> method. The +errors+ property of this exception contains an array of
|
323
|
-
# AttributeAssignmentError
|
324
|
-
# objects that should be inspected to determine which attributes triggered the errors.
|
325
250
|
# * AttributeAssignmentError - An error occurred while doing a mass assignment through the
|
326
|
-
#
|
251
|
+
# {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
|
327
252
|
# You can inspect the +attribute+ property of the exception object to determine which attribute
|
328
253
|
# triggered the error.
|
254
|
+
# * ConnectionNotEstablished - No connection has been established.
|
255
|
+
# Use {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] before querying.
|
256
|
+
# * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
|
257
|
+
# {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
|
258
|
+
# The +errors+ property of this exception contains an array of
|
259
|
+
# AttributeAssignmentError
|
260
|
+
# objects that should be inspected to determine which attributes triggered the errors.
|
261
|
+
# * RecordInvalid - raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
|
262
|
+
# {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!]
|
263
|
+
# when the record is invalid.
|
264
|
+
# * RecordNotFound - No record responded to the {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] method.
|
265
|
+
# Either the row with the given ID doesn't exist or the row didn't meet the additional restrictions.
|
266
|
+
# Some {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] calls do not raise this exception to signal
|
267
|
+
# nothing was found, please check its documentation for further details.
|
268
|
+
# * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
|
269
|
+
# * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
|
329
270
|
#
|
330
271
|
# *Note*: The attributes listed are class-level attributes (accessible from both the class and instance level).
|
331
272
|
# So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
|
332
273
|
# instances in the current object space.
|
333
274
|
class Base
|
334
|
-
##
|
335
|
-
# :singleton-method:
|
336
|
-
# Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class,
|
337
|
-
# which is then passed on to any new database connections made and which can be retrieved on both
|
338
|
-
# a class and instance level by calling +logger+.
|
339
|
-
cattr_accessor :logger, :instance_writer => false
|
340
|
-
|
341
|
-
##
|
342
|
-
# :singleton-method:
|
343
|
-
# Contains the database configuration - as is typically stored in config/database.yml -
|
344
|
-
# as a Hash.
|
345
|
-
#
|
346
|
-
# For example, the following database.yml...
|
347
|
-
#
|
348
|
-
# development:
|
349
|
-
# adapter: sqlite3
|
350
|
-
# database: db/development.sqlite3
|
351
|
-
#
|
352
|
-
# production:
|
353
|
-
# adapter: sqlite3
|
354
|
-
# database: db/production.sqlite3
|
355
|
-
#
|
356
|
-
# ...would result in ActiveRecord::Base.configurations to look like this:
|
357
|
-
#
|
358
|
-
# {
|
359
|
-
# 'development' => {
|
360
|
-
# 'adapter' => 'sqlite3',
|
361
|
-
# 'database' => 'db/development.sqlite3'
|
362
|
-
# },
|
363
|
-
# 'production' => {
|
364
|
-
# 'adapter' => 'sqlite3',
|
365
|
-
# 'database' => 'db/production.sqlite3'
|
366
|
-
# }
|
367
|
-
# }
|
368
|
-
cattr_accessor :configurations, :instance_writer => false
|
369
|
-
@@configurations = {}
|
370
|
-
|
371
|
-
##
|
372
|
-
# :singleton-method:
|
373
|
-
# Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling
|
374
|
-
# dates and times from the database. This is set to :local by default.
|
375
|
-
cattr_accessor :default_timezone, :instance_writer => false
|
376
|
-
@@default_timezone = :local
|
377
|
-
|
378
|
-
##
|
379
|
-
# :singleton-method:
|
380
|
-
# Specifies the format to use when dumping the database schema with Rails'
|
381
|
-
# Rakefile. If :sql, the schema is dumped as (potentially database-
|
382
|
-
# specific) SQL statements. If :ruby, the schema is dumped as an
|
383
|
-
# ActiveRecord::Schema file which can be loaded into any database that
|
384
|
-
# supports migrations. Use :ruby if you want to have different database
|
385
|
-
# adapters for, e.g., your development and test environments.
|
386
|
-
cattr_accessor :schema_format , :instance_writer => false
|
387
|
-
@@schema_format = :ruby
|
388
|
-
|
389
|
-
##
|
390
|
-
# :singleton-method:
|
391
|
-
# Specify whether or not to use timestamps for migration versions
|
392
|
-
cattr_accessor :timestamped_migrations , :instance_writer => false
|
393
|
-
@@timestamped_migrations = true
|
394
|
-
|
395
|
-
class << self # Class methods
|
396
|
-
def inherited(child_class) #:nodoc:
|
397
|
-
child_class.initialize_generated_modules
|
398
|
-
super
|
399
|
-
end
|
400
|
-
|
401
|
-
def initialize_generated_modules #:nodoc:
|
402
|
-
@attribute_methods_mutex = Mutex.new
|
403
|
-
|
404
|
-
# force attribute methods to be higher in inheritance hierarchy than other generated methods
|
405
|
-
generated_attribute_methods
|
406
|
-
generated_feature_methods
|
407
|
-
end
|
408
|
-
|
409
|
-
def generated_feature_methods
|
410
|
-
@generated_feature_methods ||= begin
|
411
|
-
mod = const_set(:GeneratedFeatureMethods, Module.new)
|
412
|
-
include mod
|
413
|
-
mod
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
# Returns a string like 'Post(id:integer, title:string, body:text)'
|
418
|
-
def inspect
|
419
|
-
if self == Base
|
420
|
-
super
|
421
|
-
elsif abstract_class?
|
422
|
-
"#{super}(abstract)"
|
423
|
-
elsif table_exists?
|
424
|
-
attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
|
425
|
-
"#{super}(#{attr_list})"
|
426
|
-
else
|
427
|
-
"#{super}(Table doesn't exist)"
|
428
|
-
end
|
429
|
-
end
|
430
|
-
|
431
|
-
# Overwrite the default class equality method to provide support for association proxies.
|
432
|
-
def ===(object)
|
433
|
-
object.is_a?(self)
|
434
|
-
end
|
435
|
-
|
436
|
-
def arel_table
|
437
|
-
@arel_table ||= Arel::Table.new(table_name, arel_engine)
|
438
|
-
end
|
439
|
-
|
440
|
-
def arel_engine
|
441
|
-
@arel_engine ||= begin
|
442
|
-
if self == ActiveRecord::Base
|
443
|
-
ActiveRecord::Base
|
444
|
-
else
|
445
|
-
connection_handler.retrieve_connection_pool(self) ? self : superclass.arel_engine
|
446
|
-
end
|
447
|
-
end
|
448
|
-
end
|
449
|
-
|
450
|
-
private
|
451
|
-
|
452
|
-
def relation #:nodoc:
|
453
|
-
relation = Relation.new(self, arel_table)
|
454
|
-
|
455
|
-
if finder_needs_type_condition?
|
456
|
-
relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
|
457
|
-
else
|
458
|
-
relation
|
459
|
-
end
|
460
|
-
end
|
461
|
-
end
|
462
|
-
|
463
|
-
public
|
464
|
-
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
|
465
|
-
# attributes but not yet saved (pass a hash with key names matching the associated table column names).
|
466
|
-
# In both instances, valid attribute keys are determined by the column names of the associated table --
|
467
|
-
# hence you can't have attributes that aren't part of the table columns.
|
468
|
-
#
|
469
|
-
# +initialize+ respects mass-assignment security and accepts either +:as+ or +:without_protection+ options
|
470
|
-
# in the +options+ parameter.
|
471
|
-
#
|
472
|
-
# ==== Examples
|
473
|
-
# # Instantiates a single new object
|
474
|
-
# User.new(:first_name => 'Jamie')
|
475
|
-
#
|
476
|
-
# # Instantiates a single new object using the :admin mass-assignment security role
|
477
|
-
# User.new({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
|
478
|
-
#
|
479
|
-
# # Instantiates a single new object bypassing mass-assignment security
|
480
|
-
# User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
|
481
|
-
def initialize(attributes = nil, options = {})
|
482
|
-
defaults = Hash[self.class.column_defaults.map { |k, v| [k, v.duplicable? ? v.dup : v] }]
|
483
|
-
@attributes = self.class.initialize_attributes(defaults)
|
484
|
-
@association_cache = {}
|
485
|
-
@aggregation_cache = {}
|
486
|
-
@attributes_cache = {}
|
487
|
-
@new_record = true
|
488
|
-
@readonly = false
|
489
|
-
@destroyed = false
|
490
|
-
@marked_for_destruction = false
|
491
|
-
@previously_changed = {}
|
492
|
-
@changed_attributes = {}
|
493
|
-
|
494
|
-
ensure_proper_type
|
495
|
-
|
496
|
-
populate_with_current_scope_attributes
|
497
|
-
|
498
|
-
assign_attributes(attributes, options) if attributes
|
499
|
-
|
500
|
-
yield self if block_given?
|
501
|
-
run_callbacks :initialize
|
502
|
-
end
|
503
|
-
|
504
|
-
# Initialize an empty model object from +coder+. +coder+ must contain
|
505
|
-
# the attributes necessary for initializing an empty model object. For
|
506
|
-
# example:
|
507
|
-
#
|
508
|
-
# class Post < ActiveRecord::Base
|
509
|
-
# end
|
510
|
-
#
|
511
|
-
# post = Post.allocate
|
512
|
-
# post.init_with('attributes' => { 'title' => 'hello world' })
|
513
|
-
# post.title # => 'hello world'
|
514
|
-
def init_with(coder)
|
515
|
-
@attributes = self.class.initialize_attributes(coder['attributes'])
|
516
|
-
@relation = nil
|
517
|
-
|
518
|
-
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
|
519
|
-
@association_cache = {}
|
520
|
-
@aggregation_cache = {}
|
521
|
-
@readonly = @destroyed = @marked_for_destruction = false
|
522
|
-
@new_record = false
|
523
|
-
run_callbacks :find
|
524
|
-
run_callbacks :initialize
|
525
|
-
|
526
|
-
self
|
527
|
-
end
|
528
|
-
|
529
|
-
# Duped objects have no id assigned and are treated as new records. Note
|
530
|
-
# that this is a "shallow" copy as it copies the object's attributes
|
531
|
-
# only, not its associations. The extent of a "deep" copy is application
|
532
|
-
# specific and is therefore left to the application to implement according
|
533
|
-
# to its need.
|
534
|
-
# The dup method does not preserve the timestamps (created|updated)_(at|on).
|
535
|
-
def initialize_dup(other)
|
536
|
-
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
|
537
|
-
self.class.initialize_attributes(cloned_attributes, :serialized => false)
|
538
|
-
|
539
|
-
cloned_attributes.delete(self.class.primary_key)
|
540
|
-
|
541
|
-
@attributes = cloned_attributes
|
542
|
-
|
543
|
-
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
|
544
|
-
|
545
|
-
@changed_attributes = {}
|
546
|
-
self.class.column_defaults.each do |attr, orig_value|
|
547
|
-
@changed_attributes[attr] = orig_value if _field_changed?(attr, orig_value, @attributes[attr])
|
548
|
-
end
|
549
|
-
|
550
|
-
@aggregation_cache = {}
|
551
|
-
@association_cache = {}
|
552
|
-
@attributes_cache = {}
|
553
|
-
@new_record = true
|
554
|
-
|
555
|
-
ensure_proper_type
|
556
|
-
super
|
557
|
-
end
|
558
|
-
|
559
|
-
# Backport dup from 1.9 so that initialize_dup() gets called
|
560
|
-
unless Object.respond_to?(:initialize_dup, true)
|
561
|
-
def dup # :nodoc:
|
562
|
-
copy = super
|
563
|
-
copy.initialize_dup(self)
|
564
|
-
copy
|
565
|
-
end
|
566
|
-
end
|
567
|
-
|
568
|
-
# Populate +coder+ with attributes about this record that should be
|
569
|
-
# serialized. The structure of +coder+ defined in this method is
|
570
|
-
# guaranteed to match the structure of +coder+ passed to the +init_with+
|
571
|
-
# method.
|
572
|
-
#
|
573
|
-
# Example:
|
574
|
-
#
|
575
|
-
# class Post < ActiveRecord::Base
|
576
|
-
# end
|
577
|
-
# coder = {}
|
578
|
-
# Post.new.encode_with(coder)
|
579
|
-
# coder # => { 'id' => nil, ... }
|
580
|
-
def encode_with(coder)
|
581
|
-
coder['attributes'] = attributes
|
582
|
-
end
|
583
|
-
|
584
|
-
# Returns true if +comparison_object+ is the same exact object, or +comparison_object+
|
585
|
-
# is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
|
586
|
-
#
|
587
|
-
# Note that new records are different from any other record by definition, unless the
|
588
|
-
# other record is the receiver itself. Besides, if you fetch existing records with
|
589
|
-
# +select+ and leave the ID out, you're on your own, this predicate will return false.
|
590
|
-
#
|
591
|
-
# Note also that destroying a record preserves its ID in the model instance, so deleted
|
592
|
-
# models are still comparable.
|
593
|
-
def ==(comparison_object)
|
594
|
-
super ||
|
595
|
-
comparison_object.instance_of?(self.class) &&
|
596
|
-
id.present? &&
|
597
|
-
comparison_object.id == id
|
598
|
-
end
|
599
|
-
alias :eql? :==
|
600
|
-
|
601
|
-
# Delegates to id in order to allow two records of the same type and id to work with something like:
|
602
|
-
# [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
|
603
|
-
def hash
|
604
|
-
id.hash
|
605
|
-
end
|
606
|
-
|
607
|
-
# Freeze the attributes hash such that associations are still accessible, even on destroyed records.
|
608
|
-
def freeze
|
609
|
-
@attributes.freeze; self
|
610
|
-
end
|
611
|
-
|
612
|
-
# Returns +true+ if the attributes hash has been frozen.
|
613
|
-
def frozen?
|
614
|
-
@attributes.frozen?
|
615
|
-
end
|
616
|
-
|
617
|
-
# Allows sort on objects
|
618
|
-
def <=>(other_object)
|
619
|
-
if other_object.is_a?(self.class)
|
620
|
-
self.to_key <=> other_object.to_key
|
621
|
-
else
|
622
|
-
nil
|
623
|
-
end
|
624
|
-
end
|
625
|
-
|
626
|
-
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back
|
627
|
-
# attributes will be marked as read only since they cannot be saved.
|
628
|
-
def readonly?
|
629
|
-
@readonly
|
630
|
-
end
|
631
|
-
|
632
|
-
# Marks this record as read only.
|
633
|
-
def readonly!
|
634
|
-
@readonly = true
|
635
|
-
end
|
636
|
-
|
637
|
-
# Returns the contents of the record as a nicely formatted string.
|
638
|
-
def inspect
|
639
|
-
inspection = if @attributes
|
640
|
-
self.class.column_names.collect { |name|
|
641
|
-
if has_attribute?(name)
|
642
|
-
"#{name}: #{attribute_for_inspect(name)}"
|
643
|
-
end
|
644
|
-
}.compact.join(", ")
|
645
|
-
else
|
646
|
-
"not initialized"
|
647
|
-
end
|
648
|
-
"#<#{self.class} #{inspection}>"
|
649
|
-
end
|
650
|
-
|
651
|
-
# Hackery to accomodate Syck. Remove for 4.0.
|
652
|
-
def to_yaml(opts = {}) #:nodoc:
|
653
|
-
if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
|
654
|
-
super
|
655
|
-
else
|
656
|
-
coder = {}
|
657
|
-
encode_with(coder)
|
658
|
-
YAML.quick_emit(self, opts) do |out|
|
659
|
-
out.map(taguri, to_yaml_style) do |map|
|
660
|
-
coder.each { |k, v| map.add(k, v) }
|
661
|
-
end
|
662
|
-
end
|
663
|
-
end
|
664
|
-
end
|
665
|
-
|
666
|
-
# Hackery to accomodate Syck. Remove for 4.0.
|
667
|
-
def yaml_initialize(tag, coder) #:nodoc:
|
668
|
-
init_with(coder)
|
669
|
-
end
|
670
|
-
|
671
|
-
private
|
672
|
-
|
673
|
-
# Under Ruby 1.9, Array#flatten will call #to_ary (recursively) on each of the elements
|
674
|
-
# of the array, and then rescues from the possible NoMethodError. If those elements are
|
675
|
-
# ActiveRecord::Base's, then this triggers the various method_missing's that we have,
|
676
|
-
# which significantly impacts upon performance.
|
677
|
-
#
|
678
|
-
# So we can avoid the method_missing hit by explicitly defining #to_ary as nil here.
|
679
|
-
#
|
680
|
-
# See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary/
|
681
|
-
def to_ary # :nodoc:
|
682
|
-
nil
|
683
|
-
end
|
684
|
-
|
685
|
-
include ActiveRecord::Persistence
|
686
275
|
extend ActiveModel::Naming
|
687
|
-
|
276
|
+
|
688
277
|
extend ActiveSupport::Benchmarkable
|
689
278
|
extend ActiveSupport::DescendantsTracker
|
690
279
|
|
280
|
+
extend ConnectionHandling
|
281
|
+
extend QueryCache::ClassMethods
|
691
282
|
extend Querying
|
283
|
+
extend Translation
|
284
|
+
extend DynamicMatchers
|
285
|
+
extend Explain
|
286
|
+
extend Enum
|
287
|
+
extend Delegation::DelegateCache
|
288
|
+
extend CollectionCacheKey
|
289
|
+
|
290
|
+
include Core
|
291
|
+
include Persistence
|
692
292
|
include ReadonlyAttributes
|
693
293
|
include ModelSchema
|
694
|
-
extend Translation
|
695
294
|
include Inheritance
|
696
295
|
include Scoping
|
697
|
-
extend DynamicMatchers
|
698
296
|
include Sanitization
|
699
297
|
include AttributeAssignment
|
700
298
|
include ActiveModel::Conversion
|
701
299
|
include Integration
|
702
300
|
include Validations
|
703
|
-
|
704
|
-
include
|
301
|
+
include CounterCache
|
302
|
+
include Attributes
|
303
|
+
include AttributeDecorators
|
304
|
+
include Locking::Optimistic
|
305
|
+
include Locking::Pessimistic
|
705
306
|
include AttributeMethods
|
706
|
-
include Callbacks
|
307
|
+
include Callbacks
|
308
|
+
include Timestamp
|
707
309
|
include Associations
|
708
|
-
include IdentityMap
|
709
310
|
include ActiveModel::SecurePassword
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
include
|
715
|
-
include
|
311
|
+
include AutosaveAssociation
|
312
|
+
include NestedAttributes
|
313
|
+
include Aggregations
|
314
|
+
include Transactions
|
315
|
+
include NoTouching
|
316
|
+
include TouchLater
|
317
|
+
include Reflection
|
318
|
+
include Serialization
|
319
|
+
include Store
|
320
|
+
include SecureToken
|
321
|
+
include Suppressor
|
716
322
|
end
|
717
|
-
end
|
718
323
|
|
719
|
-
|
720
|
-
|
324
|
+
ActiveSupport.run_load_hooks(:active_record, Base)
|
325
|
+
end
|