activerecord 7.0.8 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +530 -2004
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +29 -29
  5. data/examples/performance.rb +2 -2
  6. data/lib/active_record/aggregations.rb +16 -13
  7. data/lib/active_record/association_relation.rb +2 -2
  8. data/lib/active_record/associations/alias_tracker.rb +25 -19
  9. data/lib/active_record/associations/association.rb +35 -12
  10. data/lib/active_record/associations/association_scope.rb +16 -9
  11. data/lib/active_record/associations/belongs_to_association.rb +23 -8
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -2
  13. data/lib/active_record/associations/builder/association.rb +3 -3
  14. data/lib/active_record/associations/builder/belongs_to.rb +22 -8
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -7
  16. data/lib/active_record/associations/builder/has_many.rb +3 -4
  17. data/lib/active_record/associations/builder/has_one.rb +3 -4
  18. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  19. data/lib/active_record/associations/collection_association.rb +26 -14
  20. data/lib/active_record/associations/collection_proxy.rb +29 -11
  21. data/lib/active_record/associations/errors.rb +265 -0
  22. data/lib/active_record/associations/foreign_association.rb +10 -3
  23. data/lib/active_record/associations/has_many_association.rb +21 -14
  24. data/lib/active_record/associations/has_many_through_association.rb +10 -6
  25. data/lib/active_record/associations/has_one_association.rb +10 -3
  26. data/lib/active_record/associations/join_dependency/join_association.rb +27 -25
  27. data/lib/active_record/associations/join_dependency.rb +5 -5
  28. data/lib/active_record/associations/nested_error.rb +47 -0
  29. data/lib/active_record/associations/preloader/association.rb +33 -8
  30. data/lib/active_record/associations/preloader/branch.rb +7 -1
  31. data/lib/active_record/associations/preloader/through_association.rb +1 -3
  32. data/lib/active_record/associations/preloader.rb +13 -10
  33. data/lib/active_record/associations/singular_association.rb +7 -1
  34. data/lib/active_record/associations/through_association.rb +22 -11
  35. data/lib/active_record/associations.rb +328 -471
  36. data/lib/active_record/attribute_assignment.rb +1 -13
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
  38. data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
  39. data/lib/active_record/attribute_methods/dirty.rb +53 -35
  40. data/lib/active_record/attribute_methods/primary_key.rb +45 -25
  41. data/lib/active_record/attribute_methods/query.rb +28 -16
  42. data/lib/active_record/attribute_methods/read.rb +8 -7
  43. data/lib/active_record/attribute_methods/serialization.rb +131 -32
  44. data/lib/active_record/attribute_methods/time_zone_conversion.rb +7 -6
  45. data/lib/active_record/attribute_methods/write.rb +6 -6
  46. data/lib/active_record/attribute_methods.rb +148 -33
  47. data/lib/active_record/attributes.rb +58 -45
  48. data/lib/active_record/autosave_association.rb +69 -37
  49. data/lib/active_record/base.rb +9 -5
  50. data/lib/active_record/callbacks.rb +10 -24
  51. data/lib/active_record/coders/column_serializer.rb +61 -0
  52. data/lib/active_record/coders/json.rb +1 -1
  53. data/lib/active_record/coders/yaml_column.rb +70 -42
  54. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +123 -131
  55. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +4 -1
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +317 -88
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +160 -45
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +188 -63
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -63
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +306 -128
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +367 -75
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +510 -111
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +274 -125
  69. data/lib/active_record/connection_adapters/column.rb +9 -0
  70. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  71. data/lib/active_record/connection_adapters/mysql/database_statements.rb +26 -139
  72. data/lib/active_record/connection_adapters/mysql/quoting.rb +53 -54
  73. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  74. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
  76. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +25 -13
  77. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
  78. data/lib/active_record/connection_adapters/mysql2_adapter.rb +101 -68
  79. data/lib/active_record/connection_adapters/pool_config.rb +20 -10
  80. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +100 -43
  83. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +1 -1
  85. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  86. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
  87. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
  88. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +14 -4
  89. data/lib/active_record/connection_adapters/postgresql/quoting.rb +65 -61
  90. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
  91. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
  92. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +151 -2
  93. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
  94. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +368 -63
  95. data/lib/active_record/connection_adapters/postgresql_adapter.rb +364 -198
  96. data/lib/active_record/connection_adapters/schema_cache.rb +302 -79
  97. data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
  98. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +60 -43
  99. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +45 -46
  100. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
  101. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +14 -0
  102. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
  103. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +50 -8
  104. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +290 -110
  105. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  106. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  107. data/lib/active_record/connection_adapters/trilogy_adapter.rb +229 -0
  108. data/lib/active_record/connection_adapters.rb +124 -1
  109. data/lib/active_record/connection_handling.rb +96 -104
  110. data/lib/active_record/core.rb +217 -174
  111. data/lib/active_record/counter_cache.rb +68 -34
  112. data/lib/active_record/database_configurations/connection_url_resolver.rb +7 -2
  113. data/lib/active_record/database_configurations/database_config.rb +26 -5
  114. data/lib/active_record/database_configurations/hash_config.rb +52 -34
  115. data/lib/active_record/database_configurations/url_config.rb +37 -12
  116. data/lib/active_record/database_configurations.rb +87 -34
  117. data/lib/active_record/delegated_type.rb +39 -10
  118. data/lib/active_record/deprecator.rb +7 -0
  119. data/lib/active_record/destroy_association_async_job.rb +3 -1
  120. data/lib/active_record/dynamic_matchers.rb +2 -2
  121. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  122. data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
  123. data/lib/active_record/encryption/config.rb +25 -1
  124. data/lib/active_record/encryption/configurable.rb +12 -19
  125. data/lib/active_record/encryption/context.rb +10 -3
  126. data/lib/active_record/encryption/contexts.rb +5 -1
  127. data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
  128. data/lib/active_record/encryption/encryptable_record.rb +44 -20
  129. data/lib/active_record/encryption/encrypted_attribute_type.rb +45 -10
  130. data/lib/active_record/encryption/encryptor.rb +17 -2
  131. data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
  132. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
  133. data/lib/active_record/encryption/key_generator.rb +12 -1
  134. data/lib/active_record/encryption/message_pack_message_serializer.rb +76 -0
  135. data/lib/active_record/encryption/message_serializer.rb +6 -0
  136. data/lib/active_record/encryption/null_encryptor.rb +4 -0
  137. data/lib/active_record/encryption/properties.rb +3 -3
  138. data/lib/active_record/encryption/read_only_null_encryptor.rb +4 -0
  139. data/lib/active_record/encryption/scheme.rb +22 -21
  140. data/lib/active_record/encryption.rb +1 -0
  141. data/lib/active_record/enum.rb +122 -29
  142. data/lib/active_record/errors.rb +151 -31
  143. data/lib/active_record/explain.rb +21 -12
  144. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  145. data/lib/active_record/fixture_set/render_context.rb +2 -0
  146. data/lib/active_record/fixture_set/table_row.rb +29 -8
  147. data/lib/active_record/fixtures.rb +167 -97
  148. data/lib/active_record/future_result.rb +47 -8
  149. data/lib/active_record/gem_version.rb +3 -3
  150. data/lib/active_record/inheritance.rb +34 -18
  151. data/lib/active_record/insert_all.rb +72 -22
  152. data/lib/active_record/integration.rb +11 -8
  153. data/lib/active_record/internal_metadata.rb +124 -20
  154. data/lib/active_record/locking/optimistic.rb +8 -7
  155. data/lib/active_record/locking/pessimistic.rb +5 -2
  156. data/lib/active_record/log_subscriber.rb +18 -22
  157. data/lib/active_record/marshalling.rb +56 -0
  158. data/lib/active_record/message_pack.rb +124 -0
  159. data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
  160. data/lib/active_record/middleware/database_selector.rb +6 -8
  161. data/lib/active_record/middleware/shard_selector.rb +3 -1
  162. data/lib/active_record/migration/command_recorder.rb +106 -8
  163. data/lib/active_record/migration/compatibility.rb +147 -5
  164. data/lib/active_record/migration/default_strategy.rb +22 -0
  165. data/lib/active_record/migration/execution_strategy.rb +19 -0
  166. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  167. data/lib/active_record/migration.rb +234 -117
  168. data/lib/active_record/model_schema.rb +88 -103
  169. data/lib/active_record/nested_attributes.rb +35 -9
  170. data/lib/active_record/normalization.rb +163 -0
  171. data/lib/active_record/persistence.rb +168 -339
  172. data/lib/active_record/promise.rb +84 -0
  173. data/lib/active_record/query_cache.rb +19 -25
  174. data/lib/active_record/query_logs.rb +92 -52
  175. data/lib/active_record/query_logs_formatter.rb +41 -0
  176. data/lib/active_record/querying.rb +33 -8
  177. data/lib/active_record/railtie.rb +135 -86
  178. data/lib/active_record/railties/controller_runtime.rb +22 -7
  179. data/lib/active_record/railties/databases.rake +145 -154
  180. data/lib/active_record/railties/job_runtime.rb +23 -0
  181. data/lib/active_record/readonly_attributes.rb +32 -5
  182. data/lib/active_record/reflection.rb +259 -68
  183. data/lib/active_record/relation/batches/batch_enumerator.rb +20 -5
  184. data/lib/active_record/relation/batches.rb +196 -61
  185. data/lib/active_record/relation/calculations.rb +249 -92
  186. data/lib/active_record/relation/delegation.rb +30 -19
  187. data/lib/active_record/relation/finder_methods.rb +93 -18
  188. data/lib/active_record/relation/merger.rb +6 -6
  189. data/lib/active_record/relation/predicate_builder/array_handler.rb +2 -2
  190. data/lib/active_record/relation/predicate_builder/association_query_value.rb +18 -3
  191. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
  192. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  193. data/lib/active_record/relation/predicate_builder.rb +28 -16
  194. data/lib/active_record/relation/query_attribute.rb +2 -1
  195. data/lib/active_record/relation/query_methods.rb +548 -94
  196. data/lib/active_record/relation/record_fetch_warning.rb +3 -0
  197. data/lib/active_record/relation/spawn_methods.rb +5 -4
  198. data/lib/active_record/relation/where_clause.rb +7 -19
  199. data/lib/active_record/relation.rb +580 -90
  200. data/lib/active_record/result.rb +49 -48
  201. data/lib/active_record/runtime_registry.rb +63 -1
  202. data/lib/active_record/sanitization.rb +70 -25
  203. data/lib/active_record/schema.rb +8 -7
  204. data/lib/active_record/schema_dumper.rb +63 -14
  205. data/lib/active_record/schema_migration.rb +75 -24
  206. data/lib/active_record/scoping/default.rb +15 -5
  207. data/lib/active_record/scoping/named.rb +2 -2
  208. data/lib/active_record/scoping.rb +2 -1
  209. data/lib/active_record/secure_password.rb +60 -0
  210. data/lib/active_record/secure_token.rb +21 -3
  211. data/lib/active_record/signed_id.rb +27 -6
  212. data/lib/active_record/statement_cache.rb +7 -7
  213. data/lib/active_record/store.rb +8 -8
  214. data/lib/active_record/suppressor.rb +3 -1
  215. data/lib/active_record/table_metadata.rb +1 -1
  216. data/lib/active_record/tasks/database_tasks.rb +180 -119
  217. data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
  218. data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
  219. data/lib/active_record/tasks/sqlite_database_tasks.rb +16 -7
  220. data/lib/active_record/test_fixtures.rb +170 -155
  221. data/lib/active_record/testing/query_assertions.rb +121 -0
  222. data/lib/active_record/timestamp.rb +31 -17
  223. data/lib/active_record/token_for.rb +123 -0
  224. data/lib/active_record/touch_later.rb +12 -7
  225. data/lib/active_record/transaction.rb +132 -0
  226. data/lib/active_record/transactions.rb +106 -24
  227. data/lib/active_record/translation.rb +0 -2
  228. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  229. data/lib/active_record/type/internal/timezone.rb +7 -2
  230. data/lib/active_record/type/serialized.rb +1 -3
  231. data/lib/active_record/type/time.rb +4 -0
  232. data/lib/active_record/type_caster/connection.rb +4 -4
  233. data/lib/active_record/validations/absence.rb +1 -1
  234. data/lib/active_record/validations/associated.rb +9 -3
  235. data/lib/active_record/validations/numericality.rb +5 -4
  236. data/lib/active_record/validations/presence.rb +5 -28
  237. data/lib/active_record/validations/uniqueness.rb +60 -11
  238. data/lib/active_record/validations.rb +12 -5
  239. data/lib/active_record/version.rb +1 -1
  240. data/lib/active_record.rb +247 -33
  241. data/lib/arel/alias_predication.rb +1 -1
  242. data/lib/arel/collectors/bind.rb +2 -0
  243. data/lib/arel/collectors/composite.rb +7 -0
  244. data/lib/arel/collectors/sql_string.rb +1 -1
  245. data/lib/arel/collectors/substitute_binds.rb +1 -1
  246. data/lib/arel/errors.rb +10 -0
  247. data/lib/arel/factory_methods.rb +4 -0
  248. data/lib/arel/nodes/binary.rb +6 -7
  249. data/lib/arel/nodes/bound_sql_literal.rb +65 -0
  250. data/lib/arel/nodes/cte.rb +36 -0
  251. data/lib/arel/nodes/fragments.rb +35 -0
  252. data/lib/arel/nodes/homogeneous_in.rb +1 -9
  253. data/lib/arel/nodes/leading_join.rb +8 -0
  254. data/lib/arel/nodes/{and.rb → nary.rb} +5 -2
  255. data/lib/arel/nodes/node.rb +115 -5
  256. data/lib/arel/nodes/sql_literal.rb +13 -0
  257. data/lib/arel/nodes/table_alias.rb +4 -0
  258. data/lib/arel/nodes.rb +6 -2
  259. data/lib/arel/predications.rb +3 -1
  260. data/lib/arel/select_manager.rb +1 -1
  261. data/lib/arel/table.rb +9 -5
  262. data/lib/arel/tree_manager.rb +8 -3
  263. data/lib/arel/update_manager.rb +2 -1
  264. data/lib/arel/visitors/dot.rb +1 -0
  265. data/lib/arel/visitors/mysql.rb +17 -5
  266. data/lib/arel/visitors/postgresql.rb +1 -12
  267. data/lib/arel/visitors/to_sql.rb +112 -34
  268. data/lib/arel/visitors/visitor.rb +2 -2
  269. data/lib/arel.rb +21 -3
  270. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  271. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +4 -1
  272. data/lib/rails/generators/active_record/migration.rb +3 -1
  273. data/lib/rails/generators/active_record/model/USAGE +113 -0
  274. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  275. metadata +56 -14
  276. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  277. data/lib/active_record/null_relation.rb +0 -63
@@ -1,252 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecord
4
- class AssociationNotFoundError < ConfigurationError # :nodoc:
5
- attr_reader :record, :association_name
6
-
7
- def initialize(record = nil, association_name = nil)
8
- @record = record
9
- @association_name = association_name
10
- if record && association_name
11
- super("Association named '#{association_name}' was not found on #{record.class.name}; perhaps you misspelled it?")
12
- else
13
- super("Association was not found.")
14
- end
15
- end
16
-
17
- if defined?(DidYouMean::Correctable) && defined?(DidYouMean::SpellChecker)
18
- include DidYouMean::Correctable
19
-
20
- def corrections
21
- if record && association_name
22
- @corrections ||= begin
23
- maybe_these = record.class.reflections.keys
24
- DidYouMean::SpellChecker.new(dictionary: maybe_these).correct(association_name)
25
- end
26
- else
27
- []
28
- end
29
- end
30
- end
31
- end
32
-
33
- class InverseOfAssociationNotFoundError < ActiveRecordError # :nodoc:
34
- attr_reader :reflection, :associated_class
35
-
36
- def initialize(reflection = nil, associated_class = nil)
37
- if reflection
38
- @reflection = reflection
39
- @associated_class = associated_class.nil? ? reflection.klass : associated_class
40
- super("Could not find the inverse association for #{reflection.name} (#{reflection.options[:inverse_of].inspect} in #{associated_class.nil? ? reflection.class_name : associated_class.name})")
41
- else
42
- super("Could not find the inverse association.")
43
- end
44
- end
45
-
46
- if defined?(DidYouMean::Correctable) && defined?(DidYouMean::SpellChecker)
47
- include DidYouMean::Correctable
48
-
49
- def corrections
50
- if reflection && associated_class
51
- @corrections ||= begin
52
- maybe_these = associated_class.reflections.keys
53
- DidYouMean::SpellChecker.new(dictionary: maybe_these).correct(reflection.options[:inverse_of].to_s)
54
- end
55
- else
56
- []
57
- end
58
- end
59
- end
60
- end
61
-
62
- class InverseOfAssociationRecursiveError < ActiveRecordError # :nodoc:
63
- attr_reader :reflection
64
- def initialize(reflection = nil)
65
- if reflection
66
- @reflection = reflection
67
- super("Inverse association #{reflection.name} (#{reflection.options[:inverse_of].inspect} in #{reflection.class_name}) is recursive.")
68
- else
69
- super("Inverse association is recursive.")
70
- end
71
- end
72
- end
73
-
74
- class HasManyThroughAssociationNotFoundError < ActiveRecordError # :nodoc:
75
- attr_reader :owner_class, :reflection
76
-
77
- def initialize(owner_class = nil, reflection = nil)
78
- if owner_class && reflection
79
- @owner_class = owner_class
80
- @reflection = reflection
81
- super("Could not find the association #{reflection.options[:through].inspect} in model #{owner_class.name}")
82
- else
83
- super("Could not find the association.")
84
- end
85
- end
86
-
87
- if defined?(DidYouMean::Correctable) && defined?(DidYouMean::SpellChecker)
88
- include DidYouMean::Correctable
89
-
90
- def corrections
91
- if owner_class && reflection
92
- @corrections ||= begin
93
- maybe_these = owner_class.reflections.keys
94
- maybe_these -= [reflection.name.to_s] # remove failing reflection
95
- DidYouMean::SpellChecker.new(dictionary: maybe_these).correct(reflection.options[:through].to_s)
96
- end
97
- else
98
- []
99
- end
100
- end
101
- end
102
- end
103
-
104
- class HasManyThroughAssociationPolymorphicSourceError < ActiveRecordError # :nodoc:
105
- def initialize(owner_class_name = nil, reflection = nil, source_reflection = nil)
106
- if owner_class_name && reflection && source_reflection
107
- super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}' without 'source_type'. Try adding 'source_type: \"#{reflection.name.to_s.classify}\"' to 'has_many :through' definition.")
108
- else
109
- super("Cannot have a has_many :through association.")
110
- end
111
- end
112
- end
113
-
114
- class HasManyThroughAssociationPolymorphicThroughError < ActiveRecordError # :nodoc:
115
- def initialize(owner_class_name = nil, reflection = nil)
116
- if owner_class_name && reflection
117
- super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' which goes through the polymorphic association '#{owner_class_name}##{reflection.through_reflection.name}'.")
118
- else
119
- super("Cannot have a has_many :through association.")
120
- end
121
- end
122
- end
123
-
124
- class HasManyThroughAssociationPointlessSourceTypeError < ActiveRecordError # :nodoc:
125
- def initialize(owner_class_name = nil, reflection = nil, source_reflection = nil)
126
- if owner_class_name && reflection && source_reflection
127
- super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' with a :source_type option if the '#{reflection.through_reflection.class_name}##{source_reflection.name}' is not polymorphic. Try removing :source_type on your association.")
128
- else
129
- super("Cannot have a has_many :through association.")
130
- end
131
- end
132
- end
133
-
134
- class HasOneThroughCantAssociateThroughCollection < ActiveRecordError # :nodoc:
135
- def initialize(owner_class_name = nil, reflection = nil, through_reflection = nil)
136
- if owner_class_name && reflection && through_reflection
137
- super("Cannot have a has_one :through association '#{owner_class_name}##{reflection.name}' where the :through association '#{owner_class_name}##{through_reflection.name}' is a collection. Specify a has_one or belongs_to association in the :through option instead.")
138
- else
139
- super("Cannot have a has_one :through association.")
140
- end
141
- end
142
- end
143
-
144
- class HasOneAssociationPolymorphicThroughError < ActiveRecordError # :nodoc:
145
- def initialize(owner_class_name = nil, reflection = nil)
146
- if owner_class_name && reflection
147
- super("Cannot have a has_one :through association '#{owner_class_name}##{reflection.name}' which goes through the polymorphic association '#{owner_class_name}##{reflection.through_reflection.name}'.")
148
- else
149
- super("Cannot have a has_one :through association.")
150
- end
151
- end
152
- end
153
-
154
- class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError # :nodoc:
155
- def initialize(reflection = nil)
156
- if reflection
157
- through_reflection = reflection.through_reflection
158
- source_reflection_names = reflection.source_reflection_names
159
- source_associations = reflection.through_reflection.klass._reflections.keys
160
- super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')}?")
161
- else
162
- super("Could not find the source association(s).")
163
- end
164
- end
165
- end
166
-
167
- class HasManyThroughOrderError < ActiveRecordError # :nodoc:
168
- def initialize(owner_class_name = nil, reflection = nil, through_reflection = nil)
169
- if owner_class_name && reflection && through_reflection
170
- super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' which goes through '#{owner_class_name}##{through_reflection.name}' before the through association is defined.")
171
- else
172
- super("Cannot have a has_many :through association before the through association is defined.")
173
- end
174
- end
175
- end
176
-
177
- class ThroughCantAssociateThroughHasOneOrManyReflection < ActiveRecordError # :nodoc:
178
- def initialize(owner = nil, reflection = nil)
179
- if owner && reflection
180
- super("Cannot modify association '#{owner.class.name}##{reflection.name}' because the source reflection class '#{reflection.source_reflection.class_name}' is associated to '#{reflection.through_reflection.class_name}' via :#{reflection.source_reflection.macro}.")
181
- else
182
- super("Cannot modify association.")
183
- end
184
- end
185
- end
186
-
187
- class AmbiguousSourceReflectionForThroughAssociation < ActiveRecordError # :nodoc:
188
- def initialize(klass, macro, association_name, options, possible_sources)
189
- example_options = options.dup
190
- example_options[:source] = possible_sources.first
191
-
192
- super("Ambiguous source reflection for through association. Please " \
193
- "specify a :source directive on your declaration like:\n" \
194
- "\n" \
195
- " class #{klass} < ActiveRecord::Base\n" \
196
- " #{macro} :#{association_name}, #{example_options}\n" \
197
- " end"
198
- )
199
- end
200
- end
201
-
202
- class HasManyThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection # :nodoc:
203
- end
204
-
205
- class HasOneThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection # :nodoc:
206
- end
207
-
208
- class ThroughNestedAssociationsAreReadonly < ActiveRecordError # :nodoc:
209
- def initialize(owner = nil, reflection = nil)
210
- if owner && reflection
211
- super("Cannot modify association '#{owner.class.name}##{reflection.name}' because it goes through more than one other association.")
212
- else
213
- super("Through nested associations are read-only.")
214
- end
215
- end
216
- end
217
-
218
- class HasManyThroughNestedAssociationsAreReadonly < ThroughNestedAssociationsAreReadonly # :nodoc:
219
- end
220
-
221
- class HasOneThroughNestedAssociationsAreReadonly < ThroughNestedAssociationsAreReadonly # :nodoc:
222
- end
223
-
224
- # This error is raised when trying to eager load a polymorphic association using a JOIN.
225
- # Eager loading polymorphic associations is only possible with
226
- # {ActiveRecord::Relation#preload}[rdoc-ref:QueryMethods#preload].
227
- class EagerLoadPolymorphicError < ActiveRecordError
228
- def initialize(reflection = nil)
229
- if reflection
230
- super("Cannot eagerly load the polymorphic association #{reflection.name.inspect}")
231
- else
232
- super("Eager load polymorphic error.")
233
- end
234
- end
235
- end
236
-
237
- # This error is raised when trying to destroy a parent instance in N:1 or 1:1 associations
238
- # (has_many, has_one) when there is at least 1 child associated instance.
239
- # ex: if @project.tasks.size > 0, DeleteRestrictionError will be raised when trying to destroy @project
240
- class DeleteRestrictionError < ActiveRecordError # :nodoc:
241
- def initialize(name = nil)
242
- if name
243
- super("Cannot delete record because of dependent #{name}")
244
- else
245
- super("Delete restriction error.")
246
- end
247
- end
248
- end
249
-
250
4
  # See ActiveRecord::Associations::ClassMethods for documentation.
251
5
  module Associations # :nodoc:
252
6
  extend ActiveSupport::Autoload
@@ -319,8 +73,8 @@ module ActiveRecord
319
73
 
320
74
  private
321
75
  def init_internals
322
- @association_cache = {}
323
76
  super
77
+ @association_cache = {}
324
78
  end
325
79
 
326
80
  # Returns the specified association instance if it exists, +nil+ otherwise.
@@ -333,6 +87,8 @@ module ActiveRecord
333
87
  @association_cache[name] = association
334
88
  end
335
89
 
90
+ # = Active Record \Associations
91
+ #
336
92
  # \Associations are a set of macro-like class methods for tying objects together through
337
93
  # foreign keys. They express relationships like "Project has one Project Manager"
338
94
  # or "Project belongs to a Portfolio". Each macro adds a number of methods to the
@@ -349,23 +105,42 @@ module ActiveRecord
349
105
  #
350
106
  # The project class now has the following methods (and more) to ease the traversal and
351
107
  # manipulation of its relationships:
352
- # * <tt>Project#portfolio</tt>, <tt>Project#portfolio=(portfolio)</tt>, <tt>Project#reload_portfolio</tt>
353
- # * <tt>Project#project_manager</tt>, <tt>Project#project_manager=(project_manager)</tt>, <tt>Project#reload_project_manager</tt>
354
- # * <tt>Project#milestones.empty?</tt>, <tt>Project#milestones.size</tt>, <tt>Project#milestones</tt>, <tt>Project#milestones<<(milestone)</tt>,
355
- # <tt>Project#milestones.delete(milestone)</tt>, <tt>Project#milestones.destroy(milestone)</tt>, <tt>Project#milestones.find(milestone_id)</tt>,
356
- # <tt>Project#milestones.build</tt>, <tt>Project#milestones.create</tt>
357
- # * <tt>Project#categories.empty?</tt>, <tt>Project#categories.size</tt>, <tt>Project#categories</tt>, <tt>Project#categories<<(category1)</tt>,
358
- # <tt>Project#categories.delete(category1)</tt>, <tt>Project#categories.destroy(category1)</tt>
108
+ #
109
+ # project = Project.first
110
+ # project.portfolio
111
+ # project.portfolio = Portfolio.first
112
+ # project.reload_portfolio
113
+ #
114
+ # project.project_manager
115
+ # project.project_manager = ProjectManager.first
116
+ # project.reload_project_manager
117
+ #
118
+ # project.milestones.empty?
119
+ # project.milestones.size
120
+ # project.milestones
121
+ # project.milestones << Milestone.first
122
+ # project.milestones.delete(Milestone.first)
123
+ # project.milestones.destroy(Milestone.first)
124
+ # project.milestones.find(Milestone.first.id)
125
+ # project.milestones.build
126
+ # project.milestones.create
127
+ #
128
+ # project.categories.empty?
129
+ # project.categories.size
130
+ # project.categories
131
+ # project.categories << Category.first
132
+ # project.categories.delete(category1)
133
+ # project.categories.destroy(category1)
359
134
  #
360
135
  # === A word of warning
361
136
  #
362
137
  # Don't create associations that have the same name as {instance methods}[rdoc-ref:ActiveRecord::Core] of
363
- # <tt>ActiveRecord::Base</tt>. Since the association adds a method with that name to
364
- # its model, using an association with the same name as one provided by <tt>ActiveRecord::Base</tt> will override the method inherited through <tt>ActiveRecord::Base</tt> and will break things.
365
- # For instance, +attributes+ and +connection+ would be bad choices for association names, because those names already exist in the list of <tt>ActiveRecord::Base</tt> instance methods.
138
+ # +ActiveRecord::Base+. Since the association adds a method with that name to
139
+ # its model, using an association with the same name as one provided by +ActiveRecord::Base+ will override the method inherited through +ActiveRecord::Base+ and will break things.
140
+ # For instance, +attributes+ and +connection+ would be bad choices for association names, because those names already exist in the list of +ActiveRecord::Base+ instance methods.
366
141
  #
367
142
  # == Auto-generated methods
368
- # See also Instance Public methods below for more details.
143
+ # See also "Instance Public methods" below ( from #belongs_to ) for more details.
369
144
  #
370
145
  # === Singular associations (one-to-one)
371
146
  # | | belongs_to |
@@ -611,6 +386,7 @@ module ActiveRecord
611
386
  # def log_after_remove(record)
612
387
  # # ...
613
388
  # end
389
+ # end
614
390
  #
615
391
  # It's possible to stack callbacks by passing them as an array. Example:
616
392
  #
@@ -1012,7 +788,7 @@ module ActiveRecord
1012
788
  # query per addressable type.
1013
789
  # For example, if all the addressables are either of class Person or Company, then a total
1014
790
  # of 3 queries will be executed. The list of addressable types to load is determined on
1015
- # the back of the addresses loaded. This is not supported if Active Record has to fallback
791
+ # the back of the addresses loaded. This is not supported if Active Record has to fall back
1016
792
  # to the previous implementation of eager loading and will raise ActiveRecord::EagerLoadPolymorphicError.
1017
793
  # The reason is that the parent model's type is a column value so its corresponding table
1018
794
  # name cannot be put in the +FROM+/+JOIN+ clauses of that query.
@@ -1025,45 +801,45 @@ module ActiveRecord
1025
801
  # Indexes are appended for any more successive uses of the table name.
1026
802
  #
1027
803
  # Post.joins(:comments)
1028
- # # => SELECT ... FROM posts INNER JOIN comments ON ...
804
+ # # SELECT ... FROM posts INNER JOIN comments ON ...
1029
805
  # Post.joins(:special_comments) # STI
1030
- # # => SELECT ... FROM posts INNER JOIN comments ON ... AND comments.type = 'SpecialComment'
806
+ # # SELECT ... FROM posts INNER JOIN comments ON ... AND comments.type = 'SpecialComment'
1031
807
  # Post.joins(:comments, :special_comments) # special_comments is the reflection name, posts is the parent table name
1032
- # # => SELECT ... FROM posts INNER JOIN comments ON ... INNER JOIN comments special_comments_posts
808
+ # # SELECT ... FROM posts INNER JOIN comments ON ... INNER JOIN comments special_comments_posts
1033
809
  #
1034
810
  # Acts as tree example:
1035
811
  #
1036
812
  # TreeMixin.joins(:children)
1037
- # # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
813
+ # # SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
1038
814
  # TreeMixin.joins(children: :parent)
1039
- # # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
1040
- # INNER JOIN parents_mixins ...
815
+ # # SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
816
+ # # INNER JOIN parents_mixins ...
1041
817
  # TreeMixin.joins(children: {parent: :children})
1042
- # # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
1043
- # INNER JOIN parents_mixins ...
1044
- # INNER JOIN mixins childrens_mixins_2
818
+ # # SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
819
+ # # INNER JOIN parents_mixins ...
820
+ # # INNER JOIN mixins childrens_mixins_2
1045
821
  #
1046
822
  # Has and Belongs to Many join tables use the same idea, but add a <tt>_join</tt> suffix:
1047
823
  #
1048
824
  # Post.joins(:categories)
1049
- # # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
825
+ # # SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
1050
826
  # Post.joins(categories: :posts)
1051
- # # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
1052
- # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
827
+ # # SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
828
+ # # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
1053
829
  # Post.joins(categories: {posts: :categories})
1054
- # # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
1055
- # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
1056
- # INNER JOIN categories_posts categories_posts_join INNER JOIN categories categories_posts_2
830
+ # # SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
831
+ # # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
832
+ # # INNER JOIN categories_posts categories_posts_join INNER JOIN categories categories_posts_2
1057
833
  #
1058
834
  # If you wish to specify your own custom joins using ActiveRecord::QueryMethods#joins method, those table
1059
835
  # names will take precedence over the eager associations:
1060
836
  #
1061
837
  # Post.joins(:comments).joins("inner join comments ...")
1062
- # # => SELECT ... FROM posts INNER JOIN comments_posts ON ... INNER JOIN comments ...
838
+ # # SELECT ... FROM posts INNER JOIN comments_posts ON ... INNER JOIN comments ...
1063
839
  # Post.joins(:comments, :special_comments).joins("inner join comments ...")
1064
- # # => SELECT ... FROM posts INNER JOIN comments comments_posts ON ...
1065
- # INNER JOIN comments special_comments_posts ...
1066
- # INNER JOIN comments ...
840
+ # # SELECT ... FROM posts INNER JOIN comments comments_posts ON ...
841
+ # # INNER JOIN comments special_comments_posts ...
842
+ # # INNER JOIN comments ...
1067
843
  #
1068
844
  # Table aliases are automatically truncated according to the maximum length of table identifiers
1069
845
  # according to the specific database.
@@ -1144,7 +920,8 @@ module ActiveRecord
1144
920
  # belongs_to :dungeon, inverse_of: :evil_wizard
1145
921
  # end
1146
922
  #
1147
- # For more information, see the documentation for the +:inverse_of+ option.
923
+ # For more information, see the documentation for the +:inverse_of+ option and the
924
+ # {Active Record Associations guide}[https://guides.rubyonrails.org/association_basics.html#bi-directional-associations].
1148
925
  #
1149
926
  # == Deleting from associations
1150
927
  #
@@ -1166,7 +943,7 @@ module ActiveRecord
1166
943
  # specific association types. When no option is given, the behavior is to do nothing
1167
944
  # with the associated records when destroying a record.
1168
945
  #
1169
- # Note that <tt>:dependent</tt> is implemented using Rails' callback
946
+ # Note that <tt>:dependent</tt> is implemented using \Rails' callback
1170
947
  # system, which works by processing callbacks in order. Therefore, other
1171
948
  # callbacks declared either before or after the <tt>:dependent</tt> option
1172
949
  # can affect what it does.
@@ -1237,15 +1014,15 @@ module ActiveRecord
1237
1014
  # +collection+ is a placeholder for the symbol passed as the +name+ argument, so
1238
1015
  # <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.
1239
1016
  #
1240
- # [collection]
1017
+ # [<tt>collection</tt>]
1241
1018
  # Returns a Relation of all the associated objects.
1242
1019
  # An empty Relation is returned if none are found.
1243
- # [collection<<(object, ...)]
1020
+ # [<tt>collection<<(object, ...)</tt>]
1244
1021
  # Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
1245
1022
  # Note that this operation instantly fires update SQL without waiting for the save or update call on the
1246
1023
  # parent object, unless the parent object is a new record.
1247
1024
  # This will also run validations and callbacks of associated object(s).
1248
- # [collection.delete(object, ...)]
1025
+ # [<tt>collection.delete(object, ...)</tt>]
1249
1026
  # Removes one or more objects from the collection by setting their foreign keys to +NULL+.
1250
1027
  # Objects will be in addition destroyed if they're associated with <tt>dependent: :destroy</tt>,
1251
1028
  # and deleted if they're associated with <tt>dependent: :delete_all</tt>.
@@ -1253,75 +1030,84 @@ module ActiveRecord
1253
1030
  # If the <tt>:through</tt> option is used, then the join records are deleted (rather than
1254
1031
  # nullified) by default, but you can specify <tt>dependent: :destroy</tt> or
1255
1032
  # <tt>dependent: :nullify</tt> to override this.
1256
- # [collection.destroy(object, ...)]
1033
+ # [<tt>collection.destroy(object, ...)</tt>]
1257
1034
  # Removes one or more objects from the collection by running <tt>destroy</tt> on
1258
1035
  # each record, regardless of any dependent option, ensuring callbacks are run.
1259
1036
  #
1260
1037
  # If the <tt>:through</tt> option is used, then the join records are destroyed
1261
1038
  # instead, not the objects themselves.
1262
- # [collection=objects]
1039
+ # [<tt>collection=objects</tt>]
1263
1040
  # Replaces the collections content by deleting and adding objects as appropriate. If the <tt>:through</tt>
1264
1041
  # option is true callbacks in the join models are triggered except destroy callbacks, since deletion is
1265
1042
  # direct by default. You can specify <tt>dependent: :destroy</tt> or
1266
1043
  # <tt>dependent: :nullify</tt> to override this.
1267
- # [collection_singular_ids]
1044
+ # [<tt>collection_singular_ids</tt>]
1268
1045
  # Returns an array of the associated objects' ids
1269
- # [collection_singular_ids=ids]
1046
+ # [<tt>collection_singular_ids=ids</tt>]
1270
1047
  # Replace the collection with the objects identified by the primary keys in +ids+. This
1271
1048
  # method loads the models and calls <tt>collection=</tt>. See above.
1272
- # [collection.clear]
1049
+ # [<tt>collection.clear</tt>]
1273
1050
  # Removes every object from the collection. This destroys the associated objects if they
1274
1051
  # are associated with <tt>dependent: :destroy</tt>, deletes them directly from the
1275
1052
  # database if <tt>dependent: :delete_all</tt>, otherwise sets their foreign keys to +NULL+.
1276
1053
  # If the <tt>:through</tt> option is true no destroy callbacks are invoked on the join models.
1277
1054
  # Join models are directly deleted.
1278
- # [collection.empty?]
1055
+ # [<tt>collection.empty?</tt>]
1279
1056
  # Returns +true+ if there are no associated objects.
1280
- # [collection.size]
1057
+ # [<tt>collection.size</tt>]
1281
1058
  # Returns the number of associated objects.
1282
- # [collection.find(...)]
1059
+ # [<tt>collection.find(...)</tt>]
1283
1060
  # Finds an associated object according to the same rules as ActiveRecord::FinderMethods#find.
1284
- # [collection.exists?(...)]
1061
+ # [<tt>collection.exists?(...)</tt>]
1285
1062
  # Checks whether an associated object with the given conditions exists.
1286
1063
  # Uses the same rules as ActiveRecord::FinderMethods#exists?.
1287
- # [collection.build(attributes = {}, ...)]
1064
+ # [<tt>collection.build(attributes = {}, ...)</tt>]
1288
1065
  # Returns one or more new objects of the collection type that have been instantiated
1289
1066
  # with +attributes+ and linked to this object through a foreign key, but have not yet
1290
1067
  # been saved.
1291
- # [collection.create(attributes = {})]
1068
+ # [<tt>collection.create(attributes = {})</tt>]
1292
1069
  # Returns a new object of the collection type that has been instantiated
1293
1070
  # with +attributes+, linked to this object through a foreign key, and that has already
1294
1071
  # been saved (if it passed the validation). *Note*: This only works if the base model
1295
1072
  # already exists in the DB, not if it is a new (unsaved) record!
1296
- # [collection.create!(attributes = {})]
1073
+ # [<tt>collection.create!(attributes = {})</tt>]
1297
1074
  # Does the same as <tt>collection.create</tt>, but raises ActiveRecord::RecordInvalid
1298
1075
  # if the record is invalid.
1299
- # [collection.reload]
1076
+ # [<tt>collection.reload</tt>]
1300
1077
  # Returns a Relation of all of the associated objects, forcing a database read.
1301
1078
  # An empty Relation is returned if none are found.
1302
1079
  #
1303
- # === Example
1304
- #
1305
- # A <tt>Firm</tt> class declares <tt>has_many :clients</tt>, which will add:
1306
- # * <tt>Firm#clients</tt> (similar to <tt>Client.where(firm_id: id)</tt>)
1307
- # * <tt>Firm#clients<<</tt>
1308
- # * <tt>Firm#clients.delete</tt>
1309
- # * <tt>Firm#clients.destroy</tt>
1310
- # * <tt>Firm#clients=</tt>
1311
- # * <tt>Firm#client_ids</tt>
1312
- # * <tt>Firm#client_ids=</tt>
1313
- # * <tt>Firm#clients.clear</tt>
1314
- # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>)
1315
- # * <tt>Firm#clients.size</tt> (similar to <tt>Client.count "firm_id = #{id}"</tt>)
1316
- # * <tt>Firm#clients.find</tt> (similar to <tt>Client.where(firm_id: id).find(id)</tt>)
1317
- # * <tt>Firm#clients.exists?(name: 'ACME')</tt> (similar to <tt>Client.exists?(name: 'ACME', firm_id: firm.id)</tt>)
1318
- # * <tt>Firm#clients.build</tt> (similar to <tt>Client.new(firm_id: id)</tt>)
1319
- # * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new(firm_id: id); c.save; c</tt>)
1320
- # * <tt>Firm#clients.create!</tt> (similar to <tt>c = Client.new(firm_id: id); c.save!</tt>)
1321
- # * <tt>Firm#clients.reload</tt>
1080
+ # ==== Example
1081
+ #
1082
+ # class Firm < ActiveRecord::Base
1083
+ # has_many :clients
1084
+ # end
1085
+ #
1086
+ # Declaring <tt>has_many :clients</tt> adds the following methods (and more):
1087
+ #
1088
+ # firm = Firm.find(2)
1089
+ # client = Client.find(6)
1090
+ #
1091
+ # firm.clients # similar to Client.where(firm_id: 2)
1092
+ # firm.clients << client
1093
+ # firm.clients.delete(client)
1094
+ # firm.clients.destroy(client)
1095
+ # firm.clients = [client]
1096
+ # firm.client_ids
1097
+ # firm.client_ids = [6]
1098
+ # firm.clients.clear
1099
+ # firm.clients.empty? # similar to firm.clients.size == 0
1100
+ # firm.clients.size # similar to Client.count "firm_id = 2"
1101
+ # firm.clients.find # similar to Client.where(firm_id: 2).find(6)
1102
+ # firm.clients.exists?(name: 'ACME') # similar to Client.exists?(name: 'ACME', firm_id: 2)
1103
+ # firm.clients.build # similar to Client.new(firm_id: 2)
1104
+ # firm.clients.create # similar to Client.create(firm_id: 2)
1105
+ # firm.clients.create! # similar to Client.create!(firm_id: 2)
1106
+ # firm.clients.reload
1107
+ #
1322
1108
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1323
1109
  #
1324
- # === Scopes
1110
+ # ==== Scopes
1325
1111
  #
1326
1112
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1327
1113
  # lambda) to retrieve a specific set of records or customize the generated
@@ -1332,7 +1118,7 @@ module ActiveRecord
1332
1118
  # has_many :employees, -> { joins(:address) }
1333
1119
  # has_many :posts, ->(blog) { where("max_post_length > ?", blog.max_post_length) }
1334
1120
  #
1335
- # === Extensions
1121
+ # ==== Extensions
1336
1122
  #
1337
1123
  # The +extension+ argument allows you to pass a block into a has_many
1338
1124
  # association. This is useful for adding new finders, creators, and other
@@ -1346,31 +1132,31 @@ module ActiveRecord
1346
1132
  # end
1347
1133
  # end
1348
1134
  #
1349
- # === Options
1350
- # [:class_name]
1135
+ # ==== Options
1136
+ # [+:class_name+]
1351
1137
  # Specify the class name of the association. Use it only if that name can't be inferred
1352
1138
  # from the association name. So <tt>has_many :products</tt> will by default be linked
1353
1139
  # to the +Product+ class, but if the real class name is +SpecialProduct+, you'll have to
1354
1140
  # specify it with this option.
1355
- # [:foreign_key]
1141
+ # [+:foreign_key+]
1356
1142
  # Specify the foreign key used for the association. By default this is guessed to be the name
1357
1143
  # of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_many
1358
1144
  # association will use "person_id" as the default <tt>:foreign_key</tt>.
1359
1145
  #
1360
- # If you are going to modify the association (rather than just read from it), then it is
1361
- # a good idea to set the <tt>:inverse_of</tt> option.
1362
- # [:foreign_type]
1146
+ # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1147
+ # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1148
+ # [+:foreign_type+]
1363
1149
  # Specify the column used to store the associated object's type, if this is a polymorphic
1364
1150
  # association. By default this is guessed to be the name of the polymorphic association
1365
1151
  # specified on "as" option with a "_type" suffix. So a class that defines a
1366
1152
  # <tt>has_many :tags, as: :taggable</tt> association will use "taggable_type" as the
1367
1153
  # default <tt>:foreign_type</tt>.
1368
- # [:primary_key]
1154
+ # [+:primary_key+]
1369
1155
  # Specify the name of the column to use as the primary key for the association. By default this is +id+.
1370
- # [:dependent]
1156
+ # [+:dependent+]
1371
1157
  # Controls what happens to the associated objects when
1372
1158
  # their owner is destroyed. Note that these are implemented as
1373
- # callbacks, and Rails executes callbacks in order. Therefore, other
1159
+ # callbacks, and \Rails executes callbacks in order. Therefore, other
1374
1160
  # similar callbacks may affect the <tt>:dependent</tt> behavior, and the
1375
1161
  # <tt>:dependent</tt> behavior may affect other callbacks.
1376
1162
  #
@@ -1382,7 +1168,7 @@ module ActiveRecord
1382
1168
  # * <tt>:delete_all</tt> causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).
1383
1169
  # * <tt>:nullify</tt> causes the foreign keys to be set to +NULL+. Polymorphic type will also be nullified
1384
1170
  # on polymorphic associations. Callbacks are not executed.
1385
- # * <tt>:restrict_with_exception</tt> causes an <tt>ActiveRecord::DeleteRestrictionError</tt> exception to be raised if there are any associated records.
1171
+ # * <tt>:restrict_with_exception</tt> causes an ActiveRecord::DeleteRestrictionError exception to be raised if there are any associated records.
1386
1172
  # * <tt>:restrict_with_error</tt> causes an error to be added to the owner if there are any associated objects.
1387
1173
  #
1388
1174
  # If using with the <tt>:through</tt> option, the association on the join model must be
@@ -1394,12 +1180,12 @@ module ActiveRecord
1394
1180
  # <tt>has_many :comments, -> { where published: true }, dependent: :destroy</tt> and <tt>destroy</tt> is
1395
1181
  # called on a post, only published comments are destroyed. This means that any unpublished comments in the
1396
1182
  # database would still contain a foreign key pointing to the now deleted post.
1397
- # [:counter_cache]
1183
+ # [+:counter_cache+]
1398
1184
  # This option can be used to configure a custom named <tt>:counter_cache.</tt> You only need this option,
1399
1185
  # when you customized the name of your <tt>:counter_cache</tt> on the #belongs_to association.
1400
- # [:as]
1186
+ # [+:as+]
1401
1187
  # Specifies a polymorphic interface (See #belongs_to).
1402
- # [:through]
1188
+ # [+:through+]
1403
1189
  # Specifies an association through which to perform the query. This can be any other type
1404
1190
  # of association, including other <tt>:through</tt> associations. Options for <tt>:class_name</tt>,
1405
1191
  # <tt>:primary_key</tt> and <tt>:foreign_key</tt> are ignored, as the association uses the
@@ -1414,24 +1200,24 @@ module ActiveRecord
1414
1200
  # a good idea to set the <tt>:inverse_of</tt> option on the source association on the
1415
1201
  # join model. This allows associated records to be built which will automatically create
1416
1202
  # the appropriate join model records when they are saved. (See the 'Association Join Models'
1417
- # section above.)
1418
- # [:disable_joins]
1203
+ # and 'Setting Inverses' sections above.)
1204
+ # [+:disable_joins+]
1419
1205
  # Specifies whether joins should be skipped for an association. If set to true, two or more queries
1420
1206
  # will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory
1421
1207
  # due to database limitations. This option is only applicable on <tt>has_many :through</tt> associations as
1422
1208
  # +has_many+ alone do not perform a join.
1423
- # [:source]
1209
+ # [+:source+]
1424
1210
  # Specifies the source association name used by #has_many <tt>:through</tt> queries.
1425
1211
  # Only use it if the name cannot be inferred from the association.
1426
1212
  # <tt>has_many :subscribers, through: :subscriptions</tt> will look for either <tt>:subscribers</tt> or
1427
1213
  # <tt>:subscriber</tt> on Subscription, unless a <tt>:source</tt> is given.
1428
- # [:source_type]
1214
+ # [+:source_type+]
1429
1215
  # Specifies type of the source association used by #has_many <tt>:through</tt> queries where the source
1430
1216
  # association is a polymorphic #belongs_to.
1431
- # [:validate]
1217
+ # [+:validate+]
1432
1218
  # When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.
1433
1219
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1434
- # [:autosave]
1220
+ # [+:autosave+]
1435
1221
  # If true, always save the associated objects or destroy them if marked for destruction,
1436
1222
  # when saving the parent object. If false, never save or destroy the associated objects.
1437
1223
  # By default, only save associated objects that are new records. This option is implemented as a
@@ -1440,20 +1226,31 @@ module ActiveRecord
1440
1226
  #
1441
1227
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
1442
1228
  # <tt>:autosave</tt> to <tt>true</tt>.
1443
- # [:inverse_of]
1229
+ # [+:inverse_of+]
1444
1230
  # Specifies the name of the #belongs_to association on the associated object
1445
1231
  # that is the inverse of this #has_many association.
1446
1232
  # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
1447
- # [:extend]
1233
+ # [+:extend+]
1448
1234
  # Specifies a module or array of modules that will be extended into the association object returned.
1449
1235
  # Useful for defining methods on associations, especially when they should be shared between multiple
1450
1236
  # association objects.
1451
- # [:strict_loading]
1237
+ # [+:strict_loading+]
1452
1238
  # When set to +true+, enforces strict loading every time the associated record is loaded through this
1453
1239
  # association.
1454
- # [:ensuring_owner_was]
1240
+ # [+:ensuring_owner_was+]
1455
1241
  # Specifies an instance method to be called on the owner. The method must return true in order for the
1456
1242
  # associated records to be deleted in a background job.
1243
+ # [+:query_constraints+]
1244
+ # Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1245
+ # This is an optional option. By default Rails will attempt to derive the value automatically.
1246
+ # When the value is set the Array size must match associated model's primary key or +query_constraints+ size.
1247
+ # [+:index_errors+]
1248
+ # Allows differentiation of multiple validation errors from the association records, by including
1249
+ # an index in the error attribute name, e.g. +roles[2].level+.
1250
+ # When set to +true+, the index is based on association order, i.e. database order, with yet to be
1251
+ # persisted new records placed at the end.
1252
+ # When set to +:nested_attributes_order+, the index is based on the record order received by
1253
+ # nested attributes setter, when accepts_nested_attributes_for is used.
1457
1254
  #
1458
1255
  # Option examples:
1459
1256
  # has_many :comments, -> { order("posted_on") }
@@ -1466,6 +1263,8 @@ module ActiveRecord
1466
1263
  # has_many :subscribers, through: :subscriptions, source: :user
1467
1264
  # has_many :subscribers, through: :subscriptions, disable_joins: true
1468
1265
  # has_many :comments, strict_loading: true
1266
+ # has_many :comments, query_constraints: [:blog_id, :post_id]
1267
+ # has_many :comments, index_errors: :nested_attributes_order
1469
1268
  def has_many(name, scope = nil, **options, &extension)
1470
1269
  reflection = Builder::HasMany.build(self, name, scope, options, &extension)
1471
1270
  Reflection.add_reflection self, name, reflection
@@ -1481,37 +1280,48 @@ module ActiveRecord
1481
1280
  # +association+ is a placeholder for the symbol passed as the +name+ argument, so
1482
1281
  # <tt>has_one :manager</tt> would add among others <tt>manager.nil?</tt>.
1483
1282
  #
1484
- # [association]
1283
+ # [<tt>association</tt>]
1485
1284
  # Returns the associated object. +nil+ is returned if none is found.
1486
- # [association=(associate)]
1285
+ # [<tt>association=(associate)</tt>]
1487
1286
  # Assigns the associate object, extracts the primary key, sets it as the foreign key,
1488
1287
  # and saves the associate object. To avoid database inconsistencies, permanently deletes an existing
1489
1288
  # associated object when assigning a new one, even if the new one isn't saved to database.
1490
- # [build_association(attributes = {})]
1289
+ # [<tt>build_association(attributes = {})</tt>]
1491
1290
  # Returns a new object of the associated type that has been instantiated
1492
1291
  # with +attributes+ and linked to this object through a foreign key, but has not
1493
1292
  # yet been saved.
1494
- # [create_association(attributes = {})]
1293
+ # [<tt>create_association(attributes = {})</tt>]
1495
1294
  # Returns a new object of the associated type that has been instantiated
1496
1295
  # with +attributes+, linked to this object through a foreign key, and that
1497
1296
  # has already been saved (if it passed the validation).
1498
- # [create_association!(attributes = {})]
1297
+ # [<tt>create_association!(attributes = {})</tt>]
1499
1298
  # Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
1500
1299
  # if the record is invalid.
1501
- # [reload_association]
1300
+ # [<tt>reload_association</tt>]
1502
1301
  # Returns the associated object, forcing a database read.
1302
+ # [<tt>reset_association</tt>]
1303
+ # Unloads the associated object. The next access will query it from the database.
1304
+ #
1305
+ # ==== Example
1503
1306
  #
1504
- # === Example
1307
+ # class Account < ActiveRecord::Base
1308
+ # has_one :beneficiary
1309
+ # end
1310
+ #
1311
+ # Declaring <tt>has_one :beneficiary</tt> adds the following methods (and more):
1505
1312
  #
1506
- # An Account class declares <tt>has_one :beneficiary</tt>, which will add:
1507
- # * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.where(account_id: id).first</tt>)
1508
- # * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>)
1509
- # * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new(account_id: id)</tt>)
1510
- # * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save; b</tt>)
1511
- # * <tt>Account#create_beneficiary!</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save!; b</tt>)
1512
- # * <tt>Account#reload_beneficiary</tt>
1313
+ # account = Account.find(5)
1314
+ # beneficiary = Beneficiary.find(8)
1513
1315
  #
1514
- # === Scopes
1316
+ # account.beneficiary # similar to Beneficiary.find_by(account_id: 5)
1317
+ # account.beneficiary = beneficiary # similar to beneficiary.update(account_id: 5)
1318
+ # account.build_beneficiary # similar to Beneficiary.new(account_id: 5)
1319
+ # account.create_beneficiary # similar to Beneficiary.create(account_id: 5)
1320
+ # account.create_beneficiary! # similar to Beneficiary.create!(account_id: 5)
1321
+ # account.reload_beneficiary
1322
+ # account.reset_beneficiary
1323
+ #
1324
+ # ==== Scopes
1515
1325
  #
1516
1326
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1517
1327
  # lambda) to retrieve a specific record or customize the generated query
@@ -1522,16 +1332,16 @@ module ActiveRecord
1522
1332
  # has_one :employer, -> { joins(:company) }
1523
1333
  # has_one :latest_post, ->(blog) { where("created_at > ?", blog.enabled_at) }
1524
1334
  #
1525
- # === Options
1335
+ # ==== Options
1526
1336
  #
1527
1337
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1528
1338
  #
1529
1339
  # Options are:
1530
- # [:class_name]
1340
+ # [+:class_name+]
1531
1341
  # Specify the class name of the association. Use it only if that name can't be inferred
1532
1342
  # from the association name. So <tt>has_one :manager</tt> will by default be linked to the Manager class, but
1533
1343
  # if the real class name is Person, you'll have to specify it with this option.
1534
- # [:dependent]
1344
+ # [+:dependent+]
1535
1345
  # Controls what happens to the associated object when
1536
1346
  # its owner is destroyed:
1537
1347
  #
@@ -1543,28 +1353,28 @@ module ActiveRecord
1543
1353
  # * <tt>:delete</tt> causes the associated object to be deleted directly from the database (so callbacks will not execute)
1544
1354
  # * <tt>:nullify</tt> causes the foreign key to be set to +NULL+. Polymorphic type column is also nullified
1545
1355
  # on polymorphic associations. Callbacks are not executed.
1546
- # * <tt>:restrict_with_exception</tt> causes an <tt>ActiveRecord::DeleteRestrictionError</tt> exception to be raised if there is an associated record
1356
+ # * <tt>:restrict_with_exception</tt> causes an ActiveRecord::DeleteRestrictionError exception to be raised if there is an associated record
1547
1357
  # * <tt>:restrict_with_error</tt> causes an error to be added to the owner if there is an associated object
1548
1358
  #
1549
1359
  # Note that <tt>:dependent</tt> option is ignored when using <tt>:through</tt> option.
1550
- # [:foreign_key]
1360
+ # [+:foreign_key+]
1551
1361
  # Specify the foreign key used for the association. By default this is guessed to be the name
1552
1362
  # of this class in lower-case and "_id" suffixed. So a Person class that makes a #has_one association
1553
1363
  # will use "person_id" as the default <tt>:foreign_key</tt>.
1554
1364
  #
1555
- # If you are going to modify the association (rather than just read from it), then it is
1556
- # a good idea to set the <tt>:inverse_of</tt> option.
1557
- # [:foreign_type]
1365
+ # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1366
+ # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1367
+ # [+:foreign_type+]
1558
1368
  # Specify the column used to store the associated object's type, if this is a polymorphic
1559
1369
  # association. By default this is guessed to be the name of the polymorphic association
1560
1370
  # specified on "as" option with a "_type" suffix. So a class that defines a
1561
1371
  # <tt>has_one :tag, as: :taggable</tt> association will use "taggable_type" as the
1562
1372
  # default <tt>:foreign_type</tt>.
1563
- # [:primary_key]
1373
+ # [+:primary_key+]
1564
1374
  # Specify the method that returns the primary key used for the association. By default this is +id+.
1565
- # [:as]
1375
+ # [+:as+]
1566
1376
  # Specifies a polymorphic interface (See #belongs_to).
1567
- # [:through]
1377
+ # [+:through+]
1568
1378
  # Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt>,
1569
1379
  # <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
1570
1380
  # source reflection. You can only use a <tt>:through</tt> query through a #has_one
@@ -1579,49 +1389,58 @@ module ActiveRecord
1579
1389
  # a good idea to set the <tt>:inverse_of</tt> option on the source association on the
1580
1390
  # join model. This allows associated records to be built which will automatically create
1581
1391
  # the appropriate join model records when they are saved. (See the 'Association Join Models'
1582
- # section above.)
1583
- # [:disable_joins]
1392
+ # and 'Setting Inverses' sections above.)
1393
+ # [+:disable_joins+]
1584
1394
  # Specifies whether joins should be skipped for an association. If set to true, two or more queries
1585
1395
  # will be generated. Note that in some cases, if order or limit is applied, it will be done in-memory
1586
1396
  # due to database limitations. This option is only applicable on <tt>has_one :through</tt> associations as
1587
1397
  # +has_one+ alone does not perform a join.
1588
- # [:source]
1398
+ # [+:source+]
1589
1399
  # Specifies the source association name used by #has_one <tt>:through</tt> queries.
1590
1400
  # Only use it if the name cannot be inferred from the association.
1591
1401
  # <tt>has_one :favorite, through: :favorites</tt> will look for a
1592
1402
  # <tt>:favorite</tt> on Favorite, unless a <tt>:source</tt> is given.
1593
- # [:source_type]
1403
+ # [+:source_type+]
1594
1404
  # Specifies type of the source association used by #has_one <tt>:through</tt> queries where the source
1595
1405
  # association is a polymorphic #belongs_to.
1596
- # [:validate]
1406
+ # [+:validate+]
1597
1407
  # When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
1598
1408
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1599
- # [:autosave]
1600
- # If true, always save the associated object or destroy it if marked for destruction,
1601
- # when saving the parent object. If false, never save or destroy the associated object.
1602
- # By default, only save the associated object if it's a new record.
1409
+ # [+:autosave+]
1410
+ # If +true+, always saves the associated object or destroys it if marked for destruction,
1411
+ # when saving the parent object.
1412
+ # If +false+, never save or destroy the associated object.
1413
+ #
1414
+ # By default, only saves the associated object if it's a new record. Setting this option
1415
+ # to +true+ also enables validations on the associated object unless explicitly disabled
1416
+ # with <tt>validate: false</tt>. This is because saving an object with invalid associated
1417
+ # objects would fail, so any associated objects will go through validation checks.
1603
1418
  #
1604
1419
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
1605
1420
  # <tt>:autosave</tt> to <tt>true</tt>.
1606
- # [:touch]
1421
+ # [+:touch+]
1607
1422
  # If true, the associated object will be touched (the +updated_at+ / +updated_on+ attributes set to current time)
1608
1423
  # when this record is either saved or destroyed. If you specify a symbol, that attribute
1609
1424
  # will be updated with the current time in addition to the +updated_at+ / +updated_on+ attribute.
1610
1425
  # Please note that no validation will be performed when touching, and only the +after_touch+,
1611
1426
  # +after_commit+, and +after_rollback+ callbacks will be executed.
1612
- # [:inverse_of]
1427
+ # [+:inverse_of+]
1613
1428
  # Specifies the name of the #belongs_to association on the associated object
1614
1429
  # that is the inverse of this #has_one association.
1615
1430
  # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
1616
- # [:required]
1431
+ # [+:required+]
1617
1432
  # When set to +true+, the association will also have its presence validated.
1618
1433
  # This will validate the association itself, not the id. You can use
1619
1434
  # +:inverse_of+ to avoid an extra query during validation.
1620
- # [:strict_loading]
1435
+ # [+:strict_loading+]
1621
1436
  # Enforces strict loading every time the associated record is loaded through this association.
1622
- # [:ensuring_owner_was]
1437
+ # [+:ensuring_owner_was+]
1623
1438
  # Specifies an instance method to be called on the owner. The method must return true in order for the
1624
1439
  # associated records to be deleted in a background job.
1440
+ # [+:query_constraints+]
1441
+ # Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1442
+ # This is an optional option. By default Rails will attempt to derive the value automatically.
1443
+ # When the value is set the Array size must match associated model's primary key or +query_constraints+ size.
1625
1444
  #
1626
1445
  # Option examples:
1627
1446
  # has_one :credit_card, dependent: :destroy # destroys the associated credit card
@@ -1636,6 +1455,7 @@ module ActiveRecord
1636
1455
  # has_one :primary_address, -> { where(primary: true) }, through: :addressables, source: :addressable
1637
1456
  # has_one :credit_card, required: true
1638
1457
  # has_one :credit_card, strict_loading: true
1458
+ # has_one :employment_record_book, query_constraints: [:organization_id, :employee_id]
1639
1459
  def has_one(name, scope = nil, **options)
1640
1460
  reflection = Builder::HasOne.build(self, name, scope, options)
1641
1461
  Reflection.add_reflection self, name, reflection
@@ -1652,42 +1472,52 @@ module ActiveRecord
1652
1472
  # +association+ is a placeholder for the symbol passed as the +name+ argument, so
1653
1473
  # <tt>belongs_to :author</tt> would add among others <tt>author.nil?</tt>.
1654
1474
  #
1655
- # [association]
1475
+ # [<tt>association</tt>]
1656
1476
  # Returns the associated object. +nil+ is returned if none is found.
1657
- # [association=(associate)]
1477
+ # [<tt>association=(associate)</tt>]
1658
1478
  # Assigns the associate object, extracts the primary key, and sets it as the foreign key.
1659
1479
  # No modification or deletion of existing records takes place.
1660
- # [build_association(attributes = {})]
1480
+ # [<tt>build_association(attributes = {})</tt>]
1661
1481
  # Returns a new object of the associated type that has been instantiated
1662
1482
  # with +attributes+ and linked to this object through a foreign key, but has not yet been saved.
1663
- # [create_association(attributes = {})]
1483
+ # [<tt>create_association(attributes = {})</tt>]
1664
1484
  # Returns a new object of the associated type that has been instantiated
1665
1485
  # with +attributes+, linked to this object through a foreign key, and that
1666
1486
  # has already been saved (if it passed the validation).
1667
- # [create_association!(attributes = {})]
1487
+ # [<tt>create_association!(attributes = {})</tt>]
1668
1488
  # Does the same as <tt>create_association</tt>, but raises ActiveRecord::RecordInvalid
1669
1489
  # if the record is invalid.
1670
- # [reload_association]
1490
+ # [<tt>reload_association</tt>]
1671
1491
  # Returns the associated object, forcing a database read.
1672
- # [association_changed?]
1492
+ # [<tt>reset_association</tt>]
1493
+ # Unloads the associated object. The next access will query it from the database.
1494
+ # [<tt>association_changed?</tt>]
1673
1495
  # Returns true if a new associate object has been assigned and the next save will update the foreign key.
1674
- # [association_previously_changed?]
1496
+ # [<tt>association_previously_changed?</tt>]
1675
1497
  # Returns true if the previous save updated the association to reference a new associate object.
1676
1498
  #
1677
- # === Example
1678
- #
1679
- # A Post class declares <tt>belongs_to :author</tt>, which will add:
1680
- # * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
1681
- # * <tt>Post#author=(author)</tt> (similar to <tt>post.author_id = author.id</tt>)
1682
- # * <tt>Post#build_author</tt> (similar to <tt>post.author = Author.new</tt>)
1683
- # * <tt>Post#create_author</tt> (similar to <tt>post.author = Author.new; post.author.save; post.author</tt>)
1684
- # * <tt>Post#create_author!</tt> (similar to <tt>post.author = Author.new; post.author.save!; post.author</tt>)
1685
- # * <tt>Post#reload_author</tt>
1686
- # * <tt>Post#author_changed?</tt>
1687
- # * <tt>Post#author_previously_changed?</tt>
1688
- # The declaration can also include an +options+ hash to specialize the behavior of the association.
1499
+ # ==== Example
1500
+ #
1501
+ # class Post < ActiveRecord::Base
1502
+ # belongs_to :author
1503
+ # end
1504
+ #
1505
+ # Declaring <tt>belongs_to :author</tt> adds the following methods (and more):
1506
+ #
1507
+ # post = Post.find(7)
1508
+ # author = Author.find(19)
1509
+ #
1510
+ # post.author # similar to Author.find(post.author_id)
1511
+ # post.author = author # similar to post.author_id = author.id
1512
+ # post.build_author # similar to post.author = Author.new
1513
+ # post.create_author # similar to post.author = Author.new; post.author.save; post.author
1514
+ # post.create_author! # similar to post.author = Author.new; post.author.save!; post.author
1515
+ # post.reload_author
1516
+ # post.reset_author
1517
+ # post.author_changed?
1518
+ # post.author_previously_changed?
1689
1519
  #
1690
- # === Scopes
1520
+ # ==== Scopes
1691
1521
  #
1692
1522
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1693
1523
  # lambda) to retrieve a specific record or customize the generated query
@@ -1698,56 +1528,68 @@ module ActiveRecord
1698
1528
  # belongs_to :user, -> { joins(:friends) }
1699
1529
  # belongs_to :level, ->(game) { where("game_level > ?", game.current_level) }
1700
1530
  #
1701
- # === Options
1531
+ # ==== Options
1702
1532
  #
1703
- # [:class_name]
1533
+ # The declaration can also include an +options+ hash to specialize the behavior of the association.
1534
+ #
1535
+ # [+:class_name+]
1704
1536
  # Specify the class name of the association. Use it only if that name can't be inferred
1705
1537
  # from the association name. So <tt>belongs_to :author</tt> will by default be linked to the Author class, but
1706
1538
  # if the real class name is Person, you'll have to specify it with this option.
1707
- # [:foreign_key]
1539
+ # [+:foreign_key+]
1708
1540
  # Specify the foreign key used for the association. By default this is guessed to be the name
1709
1541
  # of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :person</tt>
1710
1542
  # association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly,
1711
1543
  # <tt>belongs_to :favorite_person, class_name: "Person"</tt> will use a foreign key
1712
1544
  # of "favorite_person_id".
1713
1545
  #
1714
- # If you are going to modify the association (rather than just read from it), then it is
1715
- # a good idea to set the <tt>:inverse_of</tt> option.
1716
- # [:foreign_type]
1546
+ # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1547
+ # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1548
+ # [+:foreign_type+]
1717
1549
  # Specify the column used to store the associated object's type, if this is a polymorphic
1718
1550
  # association. By default this is guessed to be the name of the association with a "_type"
1719
1551
  # suffix. So a class that defines a <tt>belongs_to :taggable, polymorphic: true</tt>
1720
1552
  # association will use "taggable_type" as the default <tt>:foreign_type</tt>.
1721
- # [:primary_key]
1553
+ # [+:primary_key+]
1722
1554
  # Specify the method that returns the primary key of associated object used for the association.
1723
1555
  # By default this is +id+.
1724
- # [:dependent]
1556
+ # [+:dependent+]
1725
1557
  # If set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to
1726
1558
  # <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method. If set to
1727
1559
  # <tt>:destroy_async</tt>, the associated object is scheduled to be destroyed in a background job.
1728
1560
  # This option should not be specified when #belongs_to is used in conjunction with
1729
1561
  # a #has_many relationship on another class because of the potential to leave
1730
1562
  # orphaned records behind.
1731
- # [:counter_cache]
1563
+ # [+:counter_cache+]
1732
1564
  # Caches the number of belonging objects on the associate class through the use of CounterCache::ClassMethods#increment_counter
1733
1565
  # and CounterCache::ClassMethods#decrement_counter. The counter cache is incremented when an object of this
1734
1566
  # class is created and decremented when it's destroyed. This requires that a column
1735
1567
  # named <tt>#{table_name}_count</tt> (such as +comments_count+ for a belonging Comment class)
1736
1568
  # is used on the associate class (such as a Post class) - that is the migration for
1737
1569
  # <tt>#{table_name}_count</tt> is created on the associate class (such that <tt>Post.comments_count</tt> will
1738
- # return the count cached, see note below). You can also specify a custom counter
1570
+ # return the count cached). You can also specify a custom counter
1739
1571
  # cache column by providing a column name instead of a +true+/+false+ value to this
1740
1572
  # option (e.g., <tt>counter_cache: :my_custom_counter</tt>.)
1741
- # Note: Specifying a counter cache will add it to that model's list of readonly attributes
1742
- # using +attr_readonly+.
1743
- # [:polymorphic]
1744
- # Specify this association is a polymorphic association by passing +true+.
1573
+ #
1574
+ # Starting to use counter caches on existing large tables can be troublesome, because the column
1575
+ # values must be backfilled separately of the column addition (to not lock the table for too long)
1576
+ # and before the use of +:counter_cache+ (otherwise methods like +size+/+any?+/etc, which use
1577
+ # counter caches internally, can produce incorrect results). To safely backfill the values while keeping
1578
+ # counter cache columns updated with the child records creation/removal and to avoid the mentioned methods
1579
+ # use the possibly incorrect counter cache column values and always get the results from the database,
1580
+ # use <tt>counter_cache: { active: false }</tt>.
1581
+ # If you also need to specify a custom column name, use <tt>counter_cache: { active: false, column: :my_custom_counter }</tt>.
1582
+ #
1745
1583
  # Note: If you've enabled the counter cache, then you may want to add the counter cache attribute
1746
1584
  # to the +attr_readonly+ list in the associated classes (e.g. <tt>class Post; attr_readonly :comments_count; end</tt>).
1747
- # [:validate]
1585
+ # [+:polymorphic+]
1586
+ # Specify this association is a polymorphic association by passing +true+.
1587
+ # Note: Since polymorphic associations rely on storing class names in the database, make sure to update the class names in the
1588
+ # <tt>*_type</tt> polymorphic type column of the corresponding rows.
1589
+ # [+:validate+]
1748
1590
  # When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
1749
1591
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1750
- # [:autosave]
1592
+ # [+:autosave+]
1751
1593
  # If true, always save the associated object or destroy it if marked for destruction, when
1752
1594
  # saving the parent object.
1753
1595
  # If false, never save or destroy the associated object.
@@ -1755,32 +1597,37 @@ module ActiveRecord
1755
1597
  #
1756
1598
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for
1757
1599
  # sets <tt>:autosave</tt> to <tt>true</tt>.
1758
- # [:touch]
1600
+ # [+:touch+]
1759
1601
  # If true, the associated object will be touched (the +updated_at+ / +updated_on+ attributes set to current time)
1760
1602
  # when this record is either saved or destroyed. If you specify a symbol, that attribute
1761
1603
  # will be updated with the current time in addition to the +updated_at+ / +updated_on+ attribute.
1762
1604
  # Please note that no validation will be performed when touching, and only the +after_touch+,
1763
1605
  # +after_commit+, and +after_rollback+ callbacks will be executed.
1764
- # [:inverse_of]
1606
+ # [+:inverse_of+]
1765
1607
  # Specifies the name of the #has_one or #has_many association on the associated
1766
1608
  # object that is the inverse of this #belongs_to association.
1767
1609
  # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
1768
- # [:optional]
1610
+ # [+:optional+]
1769
1611
  # When set to +true+, the association will not have its presence validated.
1770
- # [:required]
1612
+ # [+:required+]
1771
1613
  # When set to +true+, the association will also have its presence validated.
1772
1614
  # This will validate the association itself, not the id. You can use
1773
1615
  # +:inverse_of+ to avoid an extra query during validation.
1774
1616
  # NOTE: <tt>required</tt> is set to <tt>true</tt> by default and is deprecated. If
1775
1617
  # you don't want to have association presence validated, use <tt>optional: true</tt>.
1776
- # [:default]
1618
+ # [+:default+]
1777
1619
  # Provide a callable (i.e. proc or lambda) to specify that the association should
1778
1620
  # be initialized with a particular record before validation.
1779
- # [:strict_loading]
1621
+ # Please note that callable won't be executed if the record exists.
1622
+ # [+:strict_loading+]
1780
1623
  # Enforces strict loading every time the associated record is loaded through this association.
1781
- # [:ensuring_owner_was]
1624
+ # [+:ensuring_owner_was+]
1782
1625
  # Specifies an instance method to be called on the owner. The method must return true in order for the
1783
1626
  # associated records to be deleted in a background job.
1627
+ # [+:query_constraints+]
1628
+ # Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1629
+ # This is an optional option. By default Rails will attempt to derive the value automatically.
1630
+ # When the value is set the Array size must match associated model's primary key or +query_constraints+ size.
1784
1631
  #
1785
1632
  # Option examples:
1786
1633
  # belongs_to :firm, foreign_key: "client_of"
@@ -1796,6 +1643,7 @@ module ActiveRecord
1796
1643
  # belongs_to :user, optional: true
1797
1644
  # belongs_to :account, default: -> { company.account }
1798
1645
  # belongs_to :account, strict_loading: true
1646
+ # belongs_to :note, query_constraints: [:organization_id, :note_id]
1799
1647
  def belongs_to(name, scope = nil, **options)
1800
1648
  reflection = Builder::BelongsTo.build(self, name, scope, options)
1801
1649
  Reflection.add_reflection self, name, reflection
@@ -1818,7 +1666,7 @@ module ActiveRecord
1818
1666
  # The join table should not have a primary key or a model associated with it. You must manually generate the
1819
1667
  # join table with a migration such as this:
1820
1668
  #
1821
- # class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration[7.0]
1669
+ # class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration[7.2]
1822
1670
  # def change
1823
1671
  # create_join_table :developers, :projects
1824
1672
  # end
@@ -1833,71 +1681,80 @@ module ActiveRecord
1833
1681
  # +collection+ is a placeholder for the symbol passed as the +name+ argument, so
1834
1682
  # <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>.
1835
1683
  #
1836
- # [collection]
1684
+ # [<tt>collection</tt>]
1837
1685
  # Returns a Relation of all the associated objects.
1838
1686
  # An empty Relation is returned if none are found.
1839
- # [collection<<(object, ...)]
1687
+ # [<tt>collection<<(object, ...)</tt>]
1840
1688
  # Adds one or more objects to the collection by creating associations in the join table
1841
1689
  # (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method).
1842
1690
  # Note that this operation instantly fires update SQL without waiting for the save or update call on the
1843
1691
  # parent object, unless the parent object is a new record.
1844
- # [collection.delete(object, ...)]
1692
+ # [<tt>collection.delete(object, ...)</tt>]
1845
1693
  # Removes one or more objects from the collection by removing their associations from the join table.
1846
1694
  # This does not destroy the objects.
1847
- # [collection.destroy(object, ...)]
1695
+ # [<tt>collection.destroy(object, ...)</tt>]
1848
1696
  # Removes one or more objects from the collection by running destroy on each association in the join table, overriding any dependent option.
1849
1697
  # This does not destroy the objects.
1850
- # [collection=objects]
1698
+ # [<tt>collection=objects</tt>]
1851
1699
  # Replaces the collection's content by deleting and adding objects as appropriate.
1852
- # [collection_singular_ids]
1700
+ # [<tt>collection_singular_ids</tt>]
1853
1701
  # Returns an array of the associated objects' ids.
1854
- # [collection_singular_ids=ids]
1702
+ # [<tt>collection_singular_ids=ids</tt>]
1855
1703
  # Replace the collection by the objects identified by the primary keys in +ids+.
1856
- # [collection.clear]
1704
+ # [<tt>collection.clear</tt>]
1857
1705
  # Removes every object from the collection. This does not destroy the objects.
1858
- # [collection.empty?]
1706
+ # [<tt>collection.empty?</tt>]
1859
1707
  # Returns +true+ if there are no associated objects.
1860
- # [collection.size]
1708
+ # [<tt>collection.size</tt>]
1861
1709
  # Returns the number of associated objects.
1862
- # [collection.find(id)]
1710
+ # [<tt>collection.find(id)</tt>]
1863
1711
  # Finds an associated object responding to the +id+ and that
1864
1712
  # meets the condition that it has to be associated with this object.
1865
1713
  # Uses the same rules as ActiveRecord::FinderMethods#find.
1866
- # [collection.exists?(...)]
1714
+ # [<tt>collection.exists?(...)</tt>]
1867
1715
  # Checks whether an associated object with the given conditions exists.
1868
1716
  # Uses the same rules as ActiveRecord::FinderMethods#exists?.
1869
- # [collection.build(attributes = {})]
1717
+ # [<tt>collection.build(attributes = {})</tt>]
1870
1718
  # Returns a new object of the collection type that has been instantiated
1871
1719
  # with +attributes+ and linked to this object through the join table, but has not yet been saved.
1872
- # [collection.create(attributes = {})]
1720
+ # [<tt>collection.create(attributes = {})</tt>]
1873
1721
  # Returns a new object of the collection type that has been instantiated
1874
1722
  # with +attributes+, linked to this object through the join table, and that has already been
1875
1723
  # saved (if it passed the validation).
1876
- # [collection.reload]
1724
+ # [<tt>collection.reload</tt>]
1877
1725
  # Returns a Relation of all of the associated objects, forcing a database read.
1878
1726
  # An empty Relation is returned if none are found.
1879
1727
  #
1880
- # === Example
1881
- #
1882
- # A Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add:
1883
- # * <tt>Developer#projects</tt>
1884
- # * <tt>Developer#projects<<</tt>
1885
- # * <tt>Developer#projects.delete</tt>
1886
- # * <tt>Developer#projects.destroy</tt>
1887
- # * <tt>Developer#projects=</tt>
1888
- # * <tt>Developer#project_ids</tt>
1889
- # * <tt>Developer#project_ids=</tt>
1890
- # * <tt>Developer#projects.clear</tt>
1891
- # * <tt>Developer#projects.empty?</tt>
1892
- # * <tt>Developer#projects.size</tt>
1893
- # * <tt>Developer#projects.find(id)</tt>
1894
- # * <tt>Developer#projects.exists?(...)</tt>
1895
- # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new(developer_id: id)</tt>)
1896
- # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new(developer_id: id); c.save; c</tt>)
1897
- # * <tt>Developer#projects.reload</tt>
1728
+ # ==== Example
1729
+ #
1730
+ # class Developer < ActiveRecord::Base
1731
+ # has_and_belongs_to_many :projects
1732
+ # end
1733
+ #
1734
+ # Declaring <tt>has_and_belongs_to_many :projects</tt> adds the following methods (and more):
1735
+ #
1736
+ # developer = Developer.find(11)
1737
+ # project = Project.find(9)
1738
+ #
1739
+ # developer.projects
1740
+ # developer.projects << project
1741
+ # developer.projects.delete(project)
1742
+ # developer.projects.destroy(project)
1743
+ # developer.projects = [project]
1744
+ # developer.project_ids
1745
+ # developer.project_ids = [9]
1746
+ # developer.projects.clear
1747
+ # developer.projects.empty?
1748
+ # developer.projects.size
1749
+ # developer.projects.find(9)
1750
+ # developer.projects.exists?(9)
1751
+ # developer.projects.build # similar to Project.new(developer_id: 11)
1752
+ # developer.projects.create # similar to Project.create(developer_id: 11)
1753
+ # developer.projects.reload
1754
+ #
1898
1755
  # The declaration may include an +options+ hash to specialize the behavior of the association.
1899
1756
  #
1900
- # === Scopes
1757
+ # ==== Scopes
1901
1758
  #
1902
1759
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1903
1760
  # lambda) to retrieve a specific set of records or customize the generated
@@ -1909,7 +1766,7 @@ module ActiveRecord
1909
1766
  # where("default_category = ?", post.default_category)
1910
1767
  # }
1911
1768
  #
1912
- # === Extensions
1769
+ # ==== Extensions
1913
1770
  #
1914
1771
  # The +extension+ argument allows you to pass a block into a
1915
1772
  # has_and_belongs_to_many association. This is useful for adding new
@@ -1924,33 +1781,33 @@ module ActiveRecord
1924
1781
  # end
1925
1782
  # end
1926
1783
  #
1927
- # === Options
1784
+ # ==== Options
1928
1785
  #
1929
- # [:class_name]
1786
+ # [+:class_name+]
1930
1787
  # Specify the class name of the association. Use it only if that name can't be inferred
1931
1788
  # from the association name. So <tt>has_and_belongs_to_many :projects</tt> will by default be linked to the
1932
1789
  # Project class, but if the real class name is SuperProject, you'll have to specify it with this option.
1933
- # [:join_table]
1790
+ # [+:join_table+]
1934
1791
  # Specify the name of the join table if the default based on lexical order isn't what you want.
1935
1792
  # <b>WARNING:</b> If you're overwriting the table name of either class, the +table_name+ method
1936
1793
  # MUST be declared underneath any #has_and_belongs_to_many declaration in order to work.
1937
- # [:foreign_key]
1794
+ # [+:foreign_key+]
1938
1795
  # Specify the foreign key used for the association. By default this is guessed to be the name
1939
1796
  # of this class in lower-case and "_id" suffixed. So a Person class that makes
1940
1797
  # a #has_and_belongs_to_many association to Project will use "person_id" as the
1941
1798
  # default <tt>:foreign_key</tt>.
1942
1799
  #
1943
- # If you are going to modify the association (rather than just read from it), then it is
1944
- # a good idea to set the <tt>:inverse_of</tt> option.
1945
- # [:association_foreign_key]
1800
+ # Setting the <tt>:foreign_key</tt> option prevents automatic detection of the association's
1801
+ # inverse, so it is generally a good idea to set the <tt>:inverse_of</tt> option as well.
1802
+ # [+:association_foreign_key+]
1946
1803
  # Specify the foreign key used for the association on the receiving side of the association.
1947
1804
  # By default this is guessed to be the name of the associated class in lower-case and "_id" suffixed.
1948
1805
  # So if a Person class makes a #has_and_belongs_to_many association to Project,
1949
1806
  # the association will use "project_id" as the default <tt>:association_foreign_key</tt>.
1950
- # [:validate]
1807
+ # [+:validate+]
1951
1808
  # When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.
1952
1809
  # If you want to ensure associated objects are revalidated on every update, use +validates_associated+.
1953
- # [:autosave]
1810
+ # [+:autosave+]
1954
1811
  # If true, always save the associated objects or destroy them if marked for destruction, when
1955
1812
  # saving the parent object.
1956
1813
  # If false, never save or destroy the associated objects.
@@ -1958,7 +1815,7 @@ module ActiveRecord
1958
1815
  #
1959
1816
  # Note that NestedAttributes::ClassMethods#accepts_nested_attributes_for sets
1960
1817
  # <tt>:autosave</tt> to <tt>true</tt>.
1961
- # [:strict_loading]
1818
+ # [+:strict_loading+]
1962
1819
  # Enforces strict loading every time an associated record is loaded through this association.
1963
1820
  #
1964
1821
  # Option examples:
@@ -2003,7 +1860,7 @@ module ActiveRecord
2003
1860
  end
2004
1861
 
2005
1862
  has_many name, scope, **hm_options, &extension
2006
- _reflections[name.to_s].parent_reflection = habtm_reflection
1863
+ _reflections[name].parent_reflection = habtm_reflection
2007
1864
  end
2008
1865
  end
2009
1866
  end