activerecord 5.1.0 → 5.2.3
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 +5 -5
- data/CHANGELOG.md +596 -450
- data/MIT-LICENSE +1 -1
- data/README.rdoc +5 -5
- data/examples/performance.rb +2 -0
- data/examples/simple.rb +2 -0
- data/lib/active_record.rb +11 -4
- data/lib/active_record/aggregations.rb +6 -5
- data/lib/active_record/association_relation.rb +7 -5
- data/lib/active_record/associations.rb +77 -85
- data/lib/active_record/associations/alias_tracker.rb +23 -32
- data/lib/active_record/associations/association.rb +49 -35
- data/lib/active_record/associations/association_scope.rb +55 -55
- data/lib/active_record/associations/belongs_to_association.rb +30 -11
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
- data/lib/active_record/associations/builder/association.rb +4 -7
- data/lib/active_record/associations/builder/belongs_to.rb +21 -8
- data/lib/active_record/associations/builder/collection_association.rb +1 -1
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
- data/lib/active_record/associations/builder/has_many.rb +2 -0
- data/lib/active_record/associations/builder/has_one.rb +2 -0
- data/lib/active_record/associations/builder/singular_association.rb +2 -0
- data/lib/active_record/associations/collection_association.rb +66 -53
- data/lib/active_record/associations/collection_proxy.rb +30 -73
- data/lib/active_record/associations/foreign_association.rb +2 -0
- data/lib/active_record/associations/has_many_association.rb +13 -2
- data/lib/active_record/associations/has_many_through_association.rb +37 -19
- data/lib/active_record/associations/has_one_association.rb +14 -1
- data/lib/active_record/associations/has_one_through_association.rb +13 -8
- data/lib/active_record/associations/join_dependency.rb +52 -96
- data/lib/active_record/associations/join_dependency/join_association.rb +22 -75
- data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
- data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
- data/lib/active_record/associations/preloader.rb +17 -37
- data/lib/active_record/associations/preloader/association.rb +53 -92
- data/lib/active_record/associations/preloader/through_association.rb +72 -73
- data/lib/active_record/associations/singular_association.rb +14 -16
- data/lib/active_record/associations/through_association.rb +27 -12
- data/lib/active_record/attribute_assignment.rb +2 -5
- data/lib/active_record/attribute_decorators.rb +3 -2
- data/lib/active_record/attribute_methods.rb +65 -24
- data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
- data/lib/active_record/attribute_methods/dirty.rb +33 -216
- data/lib/active_record/attribute_methods/primary_key.rb +10 -13
- data/lib/active_record/attribute_methods/query.rb +2 -0
- data/lib/active_record/attribute_methods/read.rb +9 -3
- data/lib/active_record/attribute_methods/serialization.rb +23 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
- data/lib/active_record/attribute_methods/write.rb +22 -19
- data/lib/active_record/attributes.rb +7 -6
- data/lib/active_record/autosave_association.rb +15 -13
- data/lib/active_record/base.rb +2 -0
- data/lib/active_record/callbacks.rb +12 -6
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +2 -0
- data/lib/active_record/collection_cache_key.rb +15 -11
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +120 -39
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +192 -37
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +13 -2
- data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -25
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +65 -7
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +158 -87
- data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
- data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -98
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +126 -189
- data/lib/active_record/connection_adapters/column.rb +4 -2
- data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
- data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -15
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -23
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +258 -129
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +75 -87
- data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +90 -96
- data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
- data/lib/active_record/connection_handling.rb +4 -2
- data/lib/active_record/core.rb +41 -61
- data/lib/active_record/counter_cache.rb +20 -15
- data/lib/active_record/define_callbacks.rb +5 -3
- data/lib/active_record/dynamic_matchers.rb +9 -9
- data/lib/active_record/enum.rb +18 -13
- data/lib/active_record/errors.rb +60 -15
- data/lib/active_record/explain.rb +3 -1
- data/lib/active_record/explain_registry.rb +2 -0
- data/lib/active_record/explain_subscriber.rb +2 -0
- data/lib/active_record/fixture_set/file.rb +2 -0
- data/lib/active_record/fixtures.rb +67 -60
- data/lib/active_record/gem_version.rb +4 -2
- data/lib/active_record/inheritance.rb +49 -19
- data/lib/active_record/integration.rb +58 -19
- data/lib/active_record/internal_metadata.rb +2 -0
- data/lib/active_record/legacy_yaml_adapter.rb +3 -1
- data/lib/active_record/locking/optimistic.rb +30 -42
- data/lib/active_record/locking/pessimistic.rb +10 -7
- data/lib/active_record/log_subscriber.rb +46 -4
- data/lib/active_record/migration.rb +189 -139
- data/lib/active_record/migration/command_recorder.rb +11 -9
- data/lib/active_record/migration/compatibility.rb +81 -29
- data/lib/active_record/migration/join_table.rb +2 -0
- data/lib/active_record/model_schema.rb +74 -58
- data/lib/active_record/nested_attributes.rb +18 -6
- data/lib/active_record/no_touching.rb +3 -1
- data/lib/active_record/null_relation.rb +2 -0
- data/lib/active_record/persistence.rb +199 -54
- data/lib/active_record/query_cache.rb +8 -10
- data/lib/active_record/querying.rb +5 -3
- data/lib/active_record/railtie.rb +62 -6
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +2 -0
- data/lib/active_record/railties/databases.rake +48 -38
- data/lib/active_record/readonly_attributes.rb +3 -2
- data/lib/active_record/reflection.rb +137 -207
- data/lib/active_record/relation.rb +132 -207
- data/lib/active_record/relation/batches.rb +32 -17
- data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
- data/lib/active_record/relation/calculations.rb +66 -25
- data/lib/active_record/relation/delegation.rb +45 -29
- data/lib/active_record/relation/finder_methods.rb +76 -85
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +53 -23
- data/lib/active_record/relation/predicate_builder.rb +60 -79
- data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/query_attribute.rb +28 -2
- data/lib/active_record/relation/query_methods.rb +135 -103
- data/lib/active_record/relation/record_fetch_warning.rb +2 -0
- data/lib/active_record/relation/spawn_methods.rb +4 -2
- data/lib/active_record/relation/where_clause.rb +65 -67
- data/lib/active_record/relation/where_clause_factory.rb +5 -48
- data/lib/active_record/result.rb +2 -0
- data/lib/active_record/runtime_registry.rb +2 -0
- data/lib/active_record/sanitization.rb +129 -121
- data/lib/active_record/schema.rb +4 -2
- data/lib/active_record/schema_dumper.rb +36 -26
- data/lib/active_record/schema_migration.rb +2 -0
- data/lib/active_record/scoping.rb +12 -10
- data/lib/active_record/scoping/default.rb +10 -7
- data/lib/active_record/scoping/named.rb +40 -12
- data/lib/active_record/secure_token.rb +2 -0
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +22 -12
- data/lib/active_record/store.rb +3 -1
- data/lib/active_record/suppressor.rb +2 -0
- data/lib/active_record/table_metadata.rb +12 -3
- data/lib/active_record/tasks/database_tasks.rb +38 -26
- data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
- data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
- data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
- data/lib/active_record/timestamp.rb +13 -6
- data/lib/active_record/touch_later.rb +2 -0
- data/lib/active_record/transactions.rb +32 -27
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type.rb +4 -1
- data/lib/active_record/type/adapter_specific_registry.rb +2 -0
- data/lib/active_record/type/date.rb +2 -0
- data/lib/active_record/type/date_time.rb +2 -0
- data/lib/active_record/type/decimal_without_scale.rb +2 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
- data/lib/active_record/type/internal/timezone.rb +2 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +6 -0
- data/lib/active_record/type/text.rb +2 -0
- data/lib/active_record/type/time.rb +2 -0
- data/lib/active_record/type/type_map.rb +2 -0
- data/lib/active_record/type/unsigned_integer.rb +2 -0
- data/lib/active_record/type_caster.rb +2 -0
- data/lib/active_record/type_caster/connection.rb +2 -0
- data/lib/active_record/type_caster/map.rb +3 -1
- data/lib/active_record/validations.rb +2 -0
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +2 -0
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +2 -0
- data/lib/active_record/validations/uniqueness.rb +36 -6
- data/lib/active_record/version.rb +2 -0
- data/lib/rails/generators/active_record.rb +3 -1
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration.rb +2 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- metadata +24 -36
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- data/lib/active_record/associations/preloader/has_many.rb +0 -15
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -15
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -18
- data/lib/active_record/attribute.rb +0 -240
- data/lib/active_record/attribute/user_provided_default.rb +0 -30
- data/lib/active_record/attribute_mutation_tracker.rb +0 -113
- data/lib/active_record/attribute_set.rb +0 -113
- data/lib/active_record/attribute_set/builder.rb +0 -124
- data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
- data/lib/active_record/type/internal/abstract_json.rb +0 -33
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -26,7 +26,7 @@ The Product class is automatically mapped to the table named "products",
|
|
26
26
|
which might look like this:
|
27
27
|
|
28
28
|
CREATE TABLE products (
|
29
|
-
id
|
29
|
+
id bigint NOT NULL auto_increment,
|
30
30
|
name varchar(255),
|
31
31
|
PRIMARY KEY (id)
|
32
32
|
);
|
@@ -162,7 +162,7 @@ This would also define the following accessors: Product#name and
|
|
162
162
|
== Philosophy
|
163
163
|
|
164
164
|
Active Record is an implementation of the object-relational mapping (ORM)
|
165
|
-
pattern[
|
165
|
+
pattern[https://www.martinfowler.com/eaaCatalog/activeRecord.html] by the same
|
166
166
|
name described by Martin Fowler:
|
167
167
|
|
168
168
|
"An object that wraps a row in a database table or view,
|
@@ -192,14 +192,14 @@ The latest version of Active Record can be installed with RubyGems:
|
|
192
192
|
|
193
193
|
Source code can be downloaded as part of the Rails project on GitHub:
|
194
194
|
|
195
|
-
* https://github.com/rails/rails/tree/
|
195
|
+
* https://github.com/rails/rails/tree/5-2-stable/activerecord
|
196
196
|
|
197
197
|
|
198
198
|
== License
|
199
199
|
|
200
200
|
Active Record is released under the MIT license:
|
201
201
|
|
202
|
-
*
|
202
|
+
* https://opensource.org/licenses/MIT
|
203
203
|
|
204
204
|
|
205
205
|
== Support
|
@@ -208,7 +208,7 @@ API documentation is at:
|
|
208
208
|
|
209
209
|
* http://api.rubyonrails.org
|
210
210
|
|
211
|
-
Bug reports
|
211
|
+
Bug reports for the Ruby on Rails project can be filed here:
|
212
212
|
|
213
213
|
* https://github.com/rails/rails/issues
|
214
214
|
|
data/examples/performance.rb
CHANGED
data/examples/simple.rb
CHANGED
data/lib/active_record.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#--
|
2
|
-
# Copyright (c) 2004-
|
4
|
+
# Copyright (c) 2004-2018 David Heinemeier Hansson
|
3
5
|
#
|
4
6
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
7
|
# a copy of this software and associated documentation files (the
|
@@ -25,14 +27,14 @@ require "active_support"
|
|
25
27
|
require "active_support/rails"
|
26
28
|
require "active_model"
|
27
29
|
require "arel"
|
30
|
+
require "yaml"
|
28
31
|
|
29
32
|
require "active_record/version"
|
30
|
-
require "
|
33
|
+
require "active_model/attribute_set"
|
31
34
|
|
32
35
|
module ActiveRecord
|
33
36
|
extend ActiveSupport::Autoload
|
34
37
|
|
35
|
-
autoload :Attribute
|
36
38
|
autoload :Base
|
37
39
|
autoload :Callbacks
|
38
40
|
autoload :Core
|
@@ -102,6 +104,7 @@ module ActiveRecord
|
|
102
104
|
|
103
105
|
autoload :Result
|
104
106
|
autoload :TableMetadata
|
107
|
+
autoload :Type
|
105
108
|
end
|
106
109
|
|
107
110
|
module Coders
|
@@ -177,5 +180,9 @@ ActiveSupport.on_load(:active_record) do
|
|
177
180
|
end
|
178
181
|
|
179
182
|
ActiveSupport.on_load(:i18n) do
|
180
|
-
I18n.load_path << File.
|
183
|
+
I18n.load_path << File.expand_path("active_record/locale/en.yml", __dir__)
|
181
184
|
end
|
185
|
+
|
186
|
+
YAML.load_tags["!ruby/object:ActiveRecord::AttributeSet"] = "ActiveModel::AttributeSet"
|
187
|
+
YAML.load_tags["!ruby/object:ActiveRecord::Attribute::FromDatabase"] = "ActiveModel::Attribute::FromDatabase"
|
188
|
+
YAML.load_tags["!ruby/object:ActiveRecord::LazyAttributeHash"] = "ActiveModel::LazyAttributeHash"
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
# See ActiveRecord::Aggregations::ClassMethods for documentation
|
3
5
|
module Aggregations
|
@@ -33,7 +35,7 @@ module ActiveRecord
|
|
33
35
|
# the database).
|
34
36
|
#
|
35
37
|
# class Customer < ActiveRecord::Base
|
36
|
-
# composed_of :balance, class_name: "Money", mapping: %w(amount
|
38
|
+
# composed_of :balance, class_name: "Money", mapping: %w(balance amount)
|
37
39
|
# composed_of :address, mapping: [ %w(address_street street), %w(address_city city) ]
|
38
40
|
# end
|
39
41
|
#
|
@@ -175,9 +177,9 @@ module ActiveRecord
|
|
175
177
|
#
|
176
178
|
# Once a #composed_of relationship is specified for a model, records can be loaded from the database
|
177
179
|
# by specifying an instance of the value object in the conditions hash. The following example
|
178
|
-
# finds all customers with +
|
180
|
+
# finds all customers with +address_street+ equal to "May Street" and +address_city+ equal to "Chicago":
|
179
181
|
#
|
180
|
-
# Customer.where(
|
182
|
+
# Customer.where(address: Address.new("May Street", "Chicago"))
|
181
183
|
#
|
182
184
|
module ClassMethods
|
183
185
|
# Adds reader and writer methods for manipulating a value object:
|
@@ -210,8 +212,7 @@ module ActiveRecord
|
|
210
212
|
#
|
211
213
|
# Option examples:
|
212
214
|
# composed_of :temperature, mapping: %w(reading celsius)
|
213
|
-
# composed_of :balance, class_name: "Money", mapping: %w(balance amount)
|
214
|
-
# converter: Proc.new { |balance| balance.to_money }
|
215
|
+
# composed_of :balance, class_name: "Money", mapping: %w(balance amount)
|
215
216
|
# composed_of :address, mapping: [ %w(address_street street), %w(address_city city) ]
|
216
217
|
# composed_of :gps_location
|
217
218
|
# composed_of :gps_location, allow_nil: true
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
class AssociationRelation < Relation
|
3
|
-
def initialize(klass,
|
4
|
-
super(klass
|
5
|
+
def initialize(klass, association)
|
6
|
+
super(klass)
|
5
7
|
@association = association
|
6
8
|
end
|
7
9
|
|
@@ -29,9 +31,9 @@ module ActiveRecord
|
|
29
31
|
private
|
30
32
|
|
31
33
|
def exec_queries
|
32
|
-
super do |
|
33
|
-
@association.
|
34
|
-
yield
|
34
|
+
super do |record|
|
35
|
+
@association.set_inverse_instance_from_queries(record)
|
36
|
+
yield record if block_given?
|
35
37
|
end
|
36
38
|
end
|
37
39
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/enumerable"
|
2
4
|
require "active_support/core_ext/string/conversions"
|
3
5
|
require "active_support/core_ext/module/remove_method"
|
@@ -138,26 +140,6 @@ module ActiveRecord
|
|
138
140
|
class HasOneThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection #:nodoc:
|
139
141
|
end
|
140
142
|
|
141
|
-
class HasManyThroughCantAssociateNewRecords < ActiveRecordError #:nodoc:
|
142
|
-
def initialize(owner = nil, reflection = nil)
|
143
|
-
if owner && reflection
|
144
|
-
super("Cannot associate new records through '#{owner.class.name}##{reflection.name}' on '#{reflection.source_reflection.class_name rescue nil}##{reflection.source_reflection.name rescue nil}'. Both records must have an id in order to create the has_many :through record associating them.")
|
145
|
-
else
|
146
|
-
super("Cannot associate new records.")
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
class HasManyThroughCantDissociateNewRecords < ActiveRecordError #:nodoc:
|
152
|
-
def initialize(owner = nil, reflection = nil)
|
153
|
-
if owner && reflection
|
154
|
-
super("Cannot dissociate new records through '#{owner.class.name}##{reflection.name}' on '#{reflection.source_reflection.class_name rescue nil}##{reflection.source_reflection.name rescue nil}'. Both records must have an id in order to delete the has_many :through record associating them.")
|
155
|
-
else
|
156
|
-
super("Cannot dissociate new records.")
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
143
|
class ThroughNestedAssociationsAreReadonly < ActiveRecordError #:nodoc:
|
162
144
|
def initialize(owner = nil, reflection = nil)
|
163
145
|
if owner && reflection
|
@@ -187,16 +169,6 @@ module ActiveRecord
|
|
187
169
|
end
|
188
170
|
end
|
189
171
|
|
190
|
-
class ReadOnlyAssociation < ActiveRecordError #:nodoc:
|
191
|
-
def initialize(reflection = nil)
|
192
|
-
if reflection
|
193
|
-
super("Cannot add to a has_many :through association. Try adding to #{reflection.through_reflection.name.inspect}.")
|
194
|
-
else
|
195
|
-
super("Read-only reflection error.")
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
172
|
# This error is raised when trying to destroy a parent instance in N:1 or 1:1 associations
|
201
173
|
# (has_many, has_one) when there is at least 1 child associated instance.
|
202
174
|
# ex: if @project.tasks.size > 0, DeleteRestrictionError will be raised when trying to destroy @project
|
@@ -222,13 +194,6 @@ module ActiveRecord
|
|
222
194
|
autoload :CollectionAssociation
|
223
195
|
autoload :ForeignAssociation
|
224
196
|
autoload :CollectionProxy
|
225
|
-
|
226
|
-
autoload :BelongsToAssociation
|
227
|
-
autoload :BelongsToPolymorphicAssociation
|
228
|
-
autoload :HasManyAssociation
|
229
|
-
autoload :HasManyThroughAssociation
|
230
|
-
autoload :HasOneAssociation
|
231
|
-
autoload :HasOneThroughAssociation
|
232
197
|
autoload :ThroughAssociation
|
233
198
|
|
234
199
|
module Builder #:nodoc:
|
@@ -243,6 +208,13 @@ module ActiveRecord
|
|
243
208
|
end
|
244
209
|
|
245
210
|
eager_autoload do
|
211
|
+
autoload :BelongsToAssociation
|
212
|
+
autoload :BelongsToPolymorphicAssociation
|
213
|
+
autoload :HasManyAssociation
|
214
|
+
autoload :HasManyThroughAssociation
|
215
|
+
autoload :HasOneAssociation
|
216
|
+
autoload :HasOneThroughAssociation
|
217
|
+
|
246
218
|
autoload :Preloader
|
247
219
|
autoload :JoinDependency
|
248
220
|
autoload :AssociationScope
|
@@ -269,7 +241,7 @@ module ActiveRecord
|
|
269
241
|
association
|
270
242
|
end
|
271
243
|
|
272
|
-
def association_cached?(name) # :nodoc
|
244
|
+
def association_cached?(name) # :nodoc:
|
273
245
|
@association_cache.key?(name)
|
274
246
|
end
|
275
247
|
|
@@ -342,17 +314,18 @@ module ActiveRecord
|
|
342
314
|
# | | belongs_to |
|
343
315
|
# generated methods | belongs_to | :polymorphic | has_one
|
344
316
|
# ----------------------------------+------------+--------------+---------
|
345
|
-
# other
|
317
|
+
# other | X | X | X
|
346
318
|
# other=(other) | X | X | X
|
347
319
|
# build_other(attributes={}) | X | | X
|
348
320
|
# create_other(attributes={}) | X | | X
|
349
321
|
# create_other!(attributes={}) | X | | X
|
322
|
+
# reload_other | X | X | X
|
350
323
|
#
|
351
324
|
# === Collection associations (one-to-many / many-to-many)
|
352
325
|
# | | | has_many
|
353
326
|
# generated methods | habtm | has_many | :through
|
354
327
|
# ----------------------------------+-------+----------+----------
|
355
|
-
# others
|
328
|
+
# others | X | X | X
|
356
329
|
# others=(other,other,...) | X | X | X
|
357
330
|
# other_ids | X | X | X
|
358
331
|
# other_ids=(id,id,...) | X | X | X
|
@@ -376,6 +349,7 @@ module ActiveRecord
|
|
376
349
|
# others.exists? | X | X | X
|
377
350
|
# others.distinct | X | X | X
|
378
351
|
# others.reset | X | X | X
|
352
|
+
# others.reload | X | X | X
|
379
353
|
#
|
380
354
|
# === Overriding generated methods
|
381
355
|
#
|
@@ -479,14 +453,14 @@ module ActiveRecord
|
|
479
453
|
# The tables for these classes could look something like:
|
480
454
|
#
|
481
455
|
# CREATE TABLE users (
|
482
|
-
# id
|
483
|
-
# account_id
|
456
|
+
# id bigint NOT NULL auto_increment,
|
457
|
+
# account_id bigint default NULL,
|
484
458
|
# name varchar default NULL,
|
485
459
|
# PRIMARY KEY (id)
|
486
460
|
# )
|
487
461
|
#
|
488
462
|
# CREATE TABLE accounts (
|
489
|
-
# id
|
463
|
+
# id bigint NOT NULL auto_increment,
|
490
464
|
# name varchar default NULL,
|
491
465
|
# PRIMARY KEY (id)
|
492
466
|
# )
|
@@ -553,9 +527,8 @@ module ActiveRecord
|
|
553
527
|
# has_many :birthday_events, ->(user) { where(starts_on: user.birthday) }, class_name: 'Event'
|
554
528
|
# end
|
555
529
|
#
|
556
|
-
# Note: Joining, eager loading and preloading of these associations is not
|
530
|
+
# Note: Joining, eager loading and preloading of these associations is not possible.
|
557
531
|
# These operations happen before instance creation and the scope will be called with a +nil+ argument.
|
558
|
-
# This can lead to unexpected behavior and is deprecated.
|
559
532
|
#
|
560
533
|
# == Association callbacks
|
561
534
|
#
|
@@ -846,7 +819,7 @@ module ActiveRecord
|
|
846
819
|
# project.milestones # fetches milestones from the database
|
847
820
|
# project.milestones.size # uses the milestone cache
|
848
821
|
# project.milestones.empty? # uses the milestone cache
|
849
|
-
# project.milestones
|
822
|
+
# project.milestones.reload.size # fetches milestones from the database
|
850
823
|
# project.milestones # uses the milestone cache
|
851
824
|
#
|
852
825
|
# == Eager loading of associations
|
@@ -1088,12 +1061,6 @@ module ActiveRecord
|
|
1088
1061
|
# belongs_to :dungeon, inverse_of: :evil_wizard
|
1089
1062
|
# end
|
1090
1063
|
#
|
1091
|
-
# There are limitations to <tt>:inverse_of</tt> support:
|
1092
|
-
#
|
1093
|
-
# * does not work with <tt>:through</tt> associations.
|
1094
|
-
# * does not work with <tt>:polymorphic</tt> associations.
|
1095
|
-
# * inverse associations for #belongs_to associations #has_many are ignored.
|
1096
|
-
#
|
1097
1064
|
# For more information, see the documentation for the +:inverse_of+ option.
|
1098
1065
|
#
|
1099
1066
|
# == Deleting from associations
|
@@ -1187,9 +1154,9 @@ module ActiveRecord
|
|
1187
1154
|
# +collection+ is a placeholder for the symbol passed as the +name+ argument, so
|
1188
1155
|
# <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.
|
1189
1156
|
#
|
1190
|
-
# [collection
|
1191
|
-
# Returns
|
1192
|
-
# An empty
|
1157
|
+
# [collection]
|
1158
|
+
# Returns a Relation of all the associated objects.
|
1159
|
+
# An empty Relation is returned if none are found.
|
1193
1160
|
# [collection<<(object, ...)]
|
1194
1161
|
# Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
|
1195
1162
|
# Note that this operation instantly fires update SQL without waiting for the save or update call on the
|
@@ -1246,6 +1213,9 @@ module ActiveRecord
|
|
1246
1213
|
# [collection.create!(attributes = {})]
|
1247
1214
|
# Does the same as <tt>collection.create</tt>, but raises ActiveRecord::RecordInvalid
|
1248
1215
|
# if the record is invalid.
|
1216
|
+
# [collection.reload]
|
1217
|
+
# Returns a Relation of all of the associated objects, forcing a database read.
|
1218
|
+
# An empty Relation is returned if none are found.
|
1249
1219
|
#
|
1250
1220
|
# === Example
|
1251
1221
|
#
|
@@ -1262,9 +1232,10 @@ module ActiveRecord
|
|
1262
1232
|
# * <tt>Firm#clients.size</tt> (similar to <tt>Client.count "firm_id = #{id}"</tt>)
|
1263
1233
|
# * <tt>Firm#clients.find</tt> (similar to <tt>Client.where(firm_id: id).find(id)</tt>)
|
1264
1234
|
# * <tt>Firm#clients.exists?(name: 'ACME')</tt> (similar to <tt>Client.exists?(name: 'ACME', firm_id: firm.id)</tt>)
|
1265
|
-
# * <tt>Firm#clients.build</tt> (similar to <tt>Client.new(
|
1266
|
-
# * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new(
|
1267
|
-
# * <tt>Firm#clients.create!</tt> (similar to <tt>c = Client.new(
|
1235
|
+
# * <tt>Firm#clients.build</tt> (similar to <tt>Client.new(firm_id: id)</tt>)
|
1236
|
+
# * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new(firm_id: id); c.save; c</tt>)
|
1237
|
+
# * <tt>Firm#clients.create!</tt> (similar to <tt>c = Client.new(firm_id: id); c.save!</tt>)
|
1238
|
+
# * <tt>Firm#clients.reload</tt>
|
1268
1239
|
# The declaration can also include an +options+ hash to specialize the behavior of the association.
|
1269
1240
|
#
|
1270
1241
|
# === Scopes
|
@@ -1276,7 +1247,7 @@ module ActiveRecord
|
|
1276
1247
|
# Scope examples:
|
1277
1248
|
# has_many :comments, -> { where(author_id: 1) }
|
1278
1249
|
# has_many :employees, -> { joins(:address) }
|
1279
|
-
# has_many :posts, ->(
|
1250
|
+
# has_many :posts, ->(blog) { where("max_post_length > ?", blog.max_post_length) }
|
1280
1251
|
#
|
1281
1252
|
# === Extensions
|
1282
1253
|
#
|
@@ -1302,6 +1273,9 @@ module ActiveRecord
|
|
1302
1273
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
1303
1274
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_many
|
1304
1275
|
# association will use "person_id" as the default <tt>:foreign_key</tt>.
|
1276
|
+
#
|
1277
|
+
# If you are going to modify the association (rather than just read from it), then it is
|
1278
|
+
# a good idea to set the <tt>:inverse_of</tt> option.
|
1305
1279
|
# [:foreign_type]
|
1306
1280
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
1307
1281
|
# association. By default this is guessed to be the name of the polymorphic association
|
@@ -1375,8 +1349,7 @@ module ActiveRecord
|
|
1375
1349
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
1376
1350
|
# [:inverse_of]
|
1377
1351
|
# Specifies the name of the #belongs_to association on the associated object
|
1378
|
-
# that is the inverse of this #has_many association.
|
1379
|
-
# with <tt>:through</tt> or <tt>:as</tt> options.
|
1352
|
+
# that is the inverse of this #has_many association.
|
1380
1353
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
1381
1354
|
# [:extend]
|
1382
1355
|
# Specifies a module or array of modules that will be extended into the association object returned.
|
@@ -1392,7 +1365,7 @@ module ActiveRecord
|
|
1392
1365
|
# has_many :tags, as: :taggable
|
1393
1366
|
# has_many :reports, -> { readonly }
|
1394
1367
|
# has_many :subscribers, through: :subscriptions, source: :user
|
1395
|
-
def has_many(name, scope = nil, options
|
1368
|
+
def has_many(name, scope = nil, **options, &extension)
|
1396
1369
|
reflection = Builder::HasMany.build(self, name, scope, options, &extension)
|
1397
1370
|
Reflection.add_reflection self, name, reflection
|
1398
1371
|
end
|
@@ -1407,7 +1380,7 @@ module ActiveRecord
|
|
1407
1380
|
# +association+ is a placeholder for the symbol passed as the +name+ argument, so
|
1408
1381
|
# <tt>has_one :manager</tt> would add among others <tt>manager.nil?</tt>.
|
1409
1382
|
#
|
1410
|
-
# [association
|
1383
|
+
# [association]
|
1411
1384
|
# Returns the associated object. +nil+ is returned if none is found.
|
1412
1385
|
# [association=(associate)]
|
1413
1386
|
# Assigns the associate object, extracts the primary key, sets it as the foreign key,
|
@@ -1424,15 +1397,18 @@ module ActiveRecord
|
|
1424
1397
|
# [create_association!(attributes = {})]
|
1425
1398
|
# Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
|
1426
1399
|
# if the record is invalid.
|
1400
|
+
# [reload_association]
|
1401
|
+
# Returns the associated object, forcing a database read.
|
1427
1402
|
#
|
1428
1403
|
# === Example
|
1429
1404
|
#
|
1430
1405
|
# An Account class declares <tt>has_one :beneficiary</tt>, which will add:
|
1431
1406
|
# * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.where(account_id: id).first</tt>)
|
1432
1407
|
# * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>)
|
1433
|
-
# * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new(
|
1434
|
-
# * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new(
|
1435
|
-
# * <tt>Account#create_beneficiary!</tt> (similar to <tt>b = Beneficiary.new(
|
1408
|
+
# * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new(account_id: id)</tt>)
|
1409
|
+
# * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save; b</tt>)
|
1410
|
+
# * <tt>Account#create_beneficiary!</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save!; b</tt>)
|
1411
|
+
# * <tt>Account#reload_beneficiary</tt>
|
1436
1412
|
#
|
1437
1413
|
# === Scopes
|
1438
1414
|
#
|
@@ -1443,7 +1419,7 @@ module ActiveRecord
|
|
1443
1419
|
# Scope examples:
|
1444
1420
|
# has_one :author, -> { where(comment_id: 1) }
|
1445
1421
|
# has_one :employer, -> { joins(:company) }
|
1446
|
-
# has_one :
|
1422
|
+
# has_one :latest_post, ->(blog) { where("created_at > ?", blog.enabled_at) }
|
1447
1423
|
#
|
1448
1424
|
# === Options
|
1449
1425
|
#
|
@@ -1469,6 +1445,9 @@ module ActiveRecord
|
|
1469
1445
|
# Specify the foreign key used for the association. By default this is guessed to be the name
|
1470
1446
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_one association
|
1471
1447
|
# will use "person_id" as the default <tt>:foreign_key</tt>.
|
1448
|
+
#
|
1449
|
+
# If you are going to modify the association (rather than just read from it), then it is
|
1450
|
+
# a good idea to set the <tt>:inverse_of</tt> option.
|
1472
1451
|
# [:foreign_type]
|
1473
1452
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
1474
1453
|
# association. By default this is guessed to be the name of the polymorphic association
|
@@ -1484,6 +1463,9 @@ module ActiveRecord
|
|
1484
1463
|
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
|
1485
1464
|
# source reflection. You can only use a <tt>:through</tt> query through a #has_one
|
1486
1465
|
# or #belongs_to association on the join model.
|
1466
|
+
#
|
1467
|
+
# If you are going to modify the association (rather than just read from it), then it is
|
1468
|
+
# a good idea to set the <tt>:inverse_of</tt> option.
|
1487
1469
|
# [:source]
|
1488
1470
|
# Specifies the source association name used by #has_one <tt>:through</tt> queries.
|
1489
1471
|
# Only use it if the name cannot be inferred from the association.
|
@@ -1504,8 +1486,7 @@ module ActiveRecord
|
|
1504
1486
|
# <tt>:autosave</tt> to <tt>true</tt>.
|
1505
1487
|
# [:inverse_of]
|
1506
1488
|
# Specifies the name of the #belongs_to association on the associated object
|
1507
|
-
# that is the inverse of this #has_one association.
|
1508
|
-
# with <tt>:through</tt> or <tt>:as</tt> options.
|
1489
|
+
# that is the inverse of this #has_one association.
|
1509
1490
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
1510
1491
|
# [:required]
|
1511
1492
|
# When set to +true+, the association will also have its presence validated.
|
@@ -1523,7 +1504,7 @@ module ActiveRecord
|
|
1523
1504
|
# has_one :club, through: :membership
|
1524
1505
|
# has_one :primary_address, -> { where(primary: true) }, through: :addressables, source: :addressable
|
1525
1506
|
# has_one :credit_card, required: true
|
1526
|
-
def has_one(name, scope = nil, options
|
1507
|
+
def has_one(name, scope = nil, **options)
|
1527
1508
|
reflection = Builder::HasOne.build(self, name, scope, options)
|
1528
1509
|
Reflection.add_reflection self, name, reflection
|
1529
1510
|
end
|
@@ -1539,7 +1520,7 @@ module ActiveRecord
|
|
1539
1520
|
# +association+ is a placeholder for the symbol passed as the +name+ argument, so
|
1540
1521
|
# <tt>belongs_to :author</tt> would add among others <tt>author.nil?</tt>.
|
1541
1522
|
#
|
1542
|
-
# [association
|
1523
|
+
# [association]
|
1543
1524
|
# Returns the associated object. +nil+ is returned if none is found.
|
1544
1525
|
# [association=(associate)]
|
1545
1526
|
# Assigns the associate object, extracts the primary key, and sets it as the foreign key.
|
@@ -1553,6 +1534,8 @@ module ActiveRecord
|
|
1553
1534
|
# [create_association!(attributes = {})]
|
1554
1535
|
# Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
|
1555
1536
|
# if the record is invalid.
|
1537
|
+
# [reload_association]
|
1538
|
+
# Returns the associated object, forcing a database read.
|
1556
1539
|
#
|
1557
1540
|
# === Example
|
1558
1541
|
#
|
@@ -1562,6 +1545,7 @@ module ActiveRecord
|
|
1562
1545
|
# * <tt>Post#build_author</tt> (similar to <tt>post.author = Author.new</tt>)
|
1563
1546
|
# * <tt>Post#create_author</tt> (similar to <tt>post.author = Author.new; post.author.save; post.author</tt>)
|
1564
1547
|
# * <tt>Post#create_author!</tt> (similar to <tt>post.author = Author.new; post.author.save!; post.author</tt>)
|
1548
|
+
# * <tt>Post#reload_author</tt>
|
1565
1549
|
# The declaration can also include an +options+ hash to specialize the behavior of the association.
|
1566
1550
|
#
|
1567
1551
|
# === Scopes
|
@@ -1573,7 +1557,7 @@ module ActiveRecord
|
|
1573
1557
|
# Scope examples:
|
1574
1558
|
# belongs_to :firm, -> { where(id: 2) }
|
1575
1559
|
# belongs_to :user, -> { joins(:friends) }
|
1576
|
-
# belongs_to :level, ->(
|
1560
|
+
# belongs_to :level, ->(game) { where("game_level > ?", game.current_level) }
|
1577
1561
|
#
|
1578
1562
|
# === Options
|
1579
1563
|
#
|
@@ -1587,6 +1571,9 @@ module ActiveRecord
|
|
1587
1571
|
# association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly,
|
1588
1572
|
# <tt>belongs_to :favorite_person, class_name: "Person"</tt> will use a foreign key
|
1589
1573
|
# of "favorite_person_id".
|
1574
|
+
#
|
1575
|
+
# If you are going to modify the association (rather than just read from it), then it is
|
1576
|
+
# a good idea to set the <tt>:inverse_of</tt> option.
|
1590
1577
|
# [:foreign_type]
|
1591
1578
|
# Specify the column used to store the associated object's type, if this is a polymorphic
|
1592
1579
|
# association. By default this is guessed to be the name of the association with a "_type"
|
@@ -1636,8 +1623,7 @@ module ActiveRecord
|
|
1636
1623
|
# +after_commit+ and +after_rollback+ callbacks are executed.
|
1637
1624
|
# [:inverse_of]
|
1638
1625
|
# Specifies the name of the #has_one or #has_many association on the associated
|
1639
|
-
# object that is the inverse of this #belongs_to association.
|
1640
|
-
# combination with the <tt>:polymorphic</tt> options.
|
1626
|
+
# object that is the inverse of this #belongs_to association.
|
1641
1627
|
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
|
1642
1628
|
# [:optional]
|
1643
1629
|
# When set to +true+, the association will not have its presence validated.
|
@@ -1664,7 +1650,7 @@ module ActiveRecord
|
|
1664
1650
|
# belongs_to :company, touch: :employees_last_updated_at
|
1665
1651
|
# belongs_to :user, optional: true
|
1666
1652
|
# belongs_to :account, default: -> { company.account }
|
1667
|
-
def belongs_to(name, scope = nil, options
|
1653
|
+
def belongs_to(name, scope = nil, **options)
|
1668
1654
|
reflection = Builder::BelongsTo.build(self, name, scope, options)
|
1669
1655
|
Reflection.add_reflection self, name, reflection
|
1670
1656
|
end
|
@@ -1701,9 +1687,9 @@ module ActiveRecord
|
|
1701
1687
|
# +collection+ is a placeholder for the symbol passed as the +name+ argument, so
|
1702
1688
|
# <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>.
|
1703
1689
|
#
|
1704
|
-
# [collection
|
1705
|
-
# Returns
|
1706
|
-
# An empty
|
1690
|
+
# [collection]
|
1691
|
+
# Returns a Relation of all the associated objects.
|
1692
|
+
# An empty Relation is returned if none are found.
|
1707
1693
|
# [collection<<(object, ...)]
|
1708
1694
|
# Adds one or more objects to the collection by creating associations in the join table
|
1709
1695
|
# (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method).
|
@@ -1741,6 +1727,9 @@ module ActiveRecord
|
|
1741
1727
|
# Returns a new object of the collection type that has been instantiated
|
1742
1728
|
# with +attributes+, linked to this object through the join table, and that has already been
|
1743
1729
|
# saved (if it passed the validation).
|
1730
|
+
# [collection.reload]
|
1731
|
+
# Returns a Relation of all of the associated objects, forcing a database read.
|
1732
|
+
# An empty Relation is returned if none are found.
|
1744
1733
|
#
|
1745
1734
|
# === Example
|
1746
1735
|
#
|
@@ -1757,8 +1746,9 @@ module ActiveRecord
|
|
1757
1746
|
# * <tt>Developer#projects.size</tt>
|
1758
1747
|
# * <tt>Developer#projects.find(id)</tt>
|
1759
1748
|
# * <tt>Developer#projects.exists?(...)</tt>
|
1760
|
-
# * <tt>Developer#projects.build</tt> (similar to <tt>Project.new(
|
1761
|
-
# * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new(
|
1749
|
+
# * <tt>Developer#projects.build</tt> (similar to <tt>Project.new(developer_id: id)</tt>)
|
1750
|
+
# * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new(developer_id: id); c.save; c</tt>)
|
1751
|
+
# * <tt>Developer#projects.reload</tt>
|
1762
1752
|
# The declaration may include an +options+ hash to specialize the behavior of the association.
|
1763
1753
|
#
|
1764
1754
|
# === Scopes
|
@@ -1769,9 +1759,8 @@ module ActiveRecord
|
|
1769
1759
|
#
|
1770
1760
|
# Scope examples:
|
1771
1761
|
# has_and_belongs_to_many :projects, -> { includes(:milestones, :manager) }
|
1772
|
-
# has_and_belongs_to_many :categories, ->(
|
1773
|
-
# where("default_category = ?",
|
1774
|
-
# }
|
1762
|
+
# has_and_belongs_to_many :categories, ->(post) {
|
1763
|
+
# where("default_category = ?", post.default_category)
|
1775
1764
|
#
|
1776
1765
|
# === Extensions
|
1777
1766
|
#
|
@@ -1803,6 +1792,9 @@ module ActiveRecord
|
|
1803
1792
|
# of this class in lower-case and "_id" suffixed. So a Person class that makes
|
1804
1793
|
# a #has_and_belongs_to_many association to Project will use "person_id" as the
|
1805
1794
|
# default <tt>:foreign_key</tt>.
|
1795
|
+
#
|
1796
|
+
# If you are going to modify the association (rather than just read from it), then it is
|
1797
|
+
# a good idea to set the <tt>:inverse_of</tt> option.
|
1806
1798
|
# [:association_foreign_key]
|
1807
1799
|
# Specify the foreign key used for the association on the receiving side of the association.
|
1808
1800
|
# By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
|
@@ -1831,7 +1823,7 @@ module ActiveRecord
|
|
1831
1823
|
|
1832
1824
|
builder = Builder::HasAndBelongsToMany.new name, self, options
|
1833
1825
|
|
1834
|
-
join_model =
|
1826
|
+
join_model = builder.through_model
|
1835
1827
|
|
1836
1828
|
const_set join_model.name, join_model
|
1837
1829
|
private_constant join_model.name
|
@@ -1860,7 +1852,7 @@ module ActiveRecord
|
|
1860
1852
|
hm_options[k] = options[k] if options.key? k
|
1861
1853
|
end
|
1862
1854
|
|
1863
|
-
|
1855
|
+
has_many name, scope, hm_options, &extension
|
1864
1856
|
_reflections[name.to_s].parent_reflection = habtm_reflection
|
1865
1857
|
end
|
1866
1858
|
end
|