activerecord 3.2.22.5 → 5.2.8

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (275) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +657 -621
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +41 -46
  5. data/examples/performance.rb +55 -42
  6. data/examples/simple.rb +6 -5
  7. data/lib/active_record/aggregations.rb +264 -236
  8. data/lib/active_record/association_relation.rb +40 -0
  9. data/lib/active_record/associations/alias_tracker.rb +47 -42
  10. data/lib/active_record/associations/association.rb +127 -75
  11. data/lib/active_record/associations/association_scope.rb +126 -92
  12. data/lib/active_record/associations/belongs_to_association.rb +78 -27
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +9 -4
  14. data/lib/active_record/associations/builder/association.rb +117 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +135 -60
  16. data/lib/active_record/associations/builder/collection_association.rb +61 -54
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +120 -42
  18. data/lib/active_record/associations/builder/has_many.rb +10 -64
  19. data/lib/active_record/associations/builder/has_one.rb +19 -51
  20. data/lib/active_record/associations/builder/singular_association.rb +28 -18
  21. data/lib/active_record/associations/collection_association.rb +226 -293
  22. data/lib/active_record/associations/collection_proxy.rb +1067 -69
  23. data/lib/active_record/associations/foreign_association.rb +13 -0
  24. data/lib/active_record/associations/has_many_association.rb +83 -47
  25. data/lib/active_record/associations/has_many_through_association.rb +98 -65
  26. data/lib/active_record/associations/has_one_association.rb +57 -20
  27. data/lib/active_record/associations/has_one_through_association.rb +18 -9
  28. data/lib/active_record/associations/join_dependency/join_association.rb +48 -126
  29. data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
  30. data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
  31. data/lib/active_record/associations/join_dependency.rb +212 -164
  32. data/lib/active_record/associations/preloader/association.rb +95 -89
  33. data/lib/active_record/associations/preloader/through_association.rb +84 -44
  34. data/lib/active_record/associations/preloader.rb +123 -111
  35. data/lib/active_record/associations/singular_association.rb +33 -24
  36. data/lib/active_record/associations/through_association.rb +60 -26
  37. data/lib/active_record/associations.rb +1759 -1506
  38. data/lib/active_record/attribute_assignment.rb +60 -193
  39. data/lib/active_record/attribute_decorators.rb +90 -0
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +55 -8
  41. data/lib/active_record/attribute_methods/dirty.rb +113 -74
  42. data/lib/active_record/attribute_methods/primary_key.rb +106 -77
  43. data/lib/active_record/attribute_methods/query.rb +8 -5
  44. data/lib/active_record/attribute_methods/read.rb +63 -114
  45. data/lib/active_record/attribute_methods/serialization.rb +60 -90
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -43
  47. data/lib/active_record/attribute_methods/write.rb +43 -45
  48. data/lib/active_record/attribute_methods.rb +366 -149
  49. data/lib/active_record/attributes.rb +266 -0
  50. data/lib/active_record/autosave_association.rb +312 -225
  51. data/lib/active_record/base.rb +114 -505
  52. data/lib/active_record/callbacks.rb +145 -67
  53. data/lib/active_record/coders/json.rb +15 -0
  54. data/lib/active_record/coders/yaml_column.rb +32 -23
  55. data/lib/active_record/collection_cache_key.rb +53 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +883 -284
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +16 -2
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +350 -200
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +82 -27
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +150 -65
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +477 -284
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1100 -310
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +450 -118
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +657 -446
  69. data/lib/active_record/connection_adapters/column.rb +50 -255
  70. data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +59 -210
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  108. data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
  109. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
  114. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
  115. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  116. data/lib/active_record/connection_adapters/postgresql_adapter.rb +620 -1080
  117. data/lib/active_record/connection_adapters/schema_cache.rb +85 -36
  118. data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +545 -27
  126. data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
  127. data/lib/active_record/connection_handling.rb +145 -0
  128. data/lib/active_record/core.rb +559 -0
  129. data/lib/active_record/counter_cache.rb +200 -105
  130. data/lib/active_record/define_callbacks.rb +22 -0
  131. data/lib/active_record/dynamic_matchers.rb +107 -69
  132. data/lib/active_record/enum.rb +244 -0
  133. data/lib/active_record/errors.rb +245 -60
  134. data/lib/active_record/explain.rb +35 -71
  135. data/lib/active_record/explain_registry.rb +32 -0
  136. data/lib/active_record/explain_subscriber.rb +18 -9
  137. data/lib/active_record/fixture_set/file.rb +82 -0
  138. data/lib/active_record/fixtures.rb +418 -275
  139. data/lib/active_record/gem_version.rb +17 -0
  140. data/lib/active_record/inheritance.rb +209 -100
  141. data/lib/active_record/integration.rb +116 -21
  142. data/lib/active_record/internal_metadata.rb +45 -0
  143. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  144. data/lib/active_record/locale/en.yml +9 -1
  145. data/lib/active_record/locking/optimistic.rb +107 -94
  146. data/lib/active_record/locking/pessimistic.rb +20 -8
  147. data/lib/active_record/log_subscriber.rb +99 -34
  148. data/lib/active_record/migration/command_recorder.rb +199 -64
  149. data/lib/active_record/migration/compatibility.rb +217 -0
  150. data/lib/active_record/migration/join_table.rb +17 -0
  151. data/lib/active_record/migration.rb +893 -296
  152. data/lib/active_record/model_schema.rb +328 -175
  153. data/lib/active_record/nested_attributes.rb +338 -242
  154. data/lib/active_record/no_touching.rb +58 -0
  155. data/lib/active_record/null_relation.rb +68 -0
  156. data/lib/active_record/persistence.rb +557 -170
  157. data/lib/active_record/query_cache.rb +14 -43
  158. data/lib/active_record/querying.rb +36 -24
  159. data/lib/active_record/railtie.rb +147 -52
  160. data/lib/active_record/railties/console_sandbox.rb +5 -4
  161. data/lib/active_record/railties/controller_runtime.rb +13 -6
  162. data/lib/active_record/railties/databases.rake +206 -488
  163. data/lib/active_record/readonly_attributes.rb +4 -6
  164. data/lib/active_record/reflection.rb +734 -228
  165. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  166. data/lib/active_record/relation/batches.rb +249 -52
  167. data/lib/active_record/relation/calculations.rb +330 -284
  168. data/lib/active_record/relation/delegation.rb +135 -37
  169. data/lib/active_record/relation/finder_methods.rb +450 -287
  170. data/lib/active_record/relation/from_clause.rb +26 -0
  171. data/lib/active_record/relation/merger.rb +193 -0
  172. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  173. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  174. data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  179. data/lib/active_record/relation/predicate_builder.rb +132 -43
  180. data/lib/active_record/relation/query_attribute.rb +45 -0
  181. data/lib/active_record/relation/query_methods.rb +1037 -221
  182. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  183. data/lib/active_record/relation/spawn_methods.rb +48 -151
  184. data/lib/active_record/relation/where_clause.rb +186 -0
  185. data/lib/active_record/relation/where_clause_factory.rb +34 -0
  186. data/lib/active_record/relation.rb +451 -359
  187. data/lib/active_record/result.rb +129 -20
  188. data/lib/active_record/runtime_registry.rb +24 -0
  189. data/lib/active_record/sanitization.rb +164 -136
  190. data/lib/active_record/schema.rb +31 -19
  191. data/lib/active_record/schema_dumper.rb +154 -107
  192. data/lib/active_record/schema_migration.rb +56 -0
  193. data/lib/active_record/scoping/default.rb +108 -98
  194. data/lib/active_record/scoping/named.rb +125 -112
  195. data/lib/active_record/scoping.rb +77 -123
  196. data/lib/active_record/secure_token.rb +40 -0
  197. data/lib/active_record/serialization.rb +10 -6
  198. data/lib/active_record/statement_cache.rb +121 -0
  199. data/lib/active_record/store.rb +175 -16
  200. data/lib/active_record/suppressor.rb +61 -0
  201. data/lib/active_record/table_metadata.rb +82 -0
  202. data/lib/active_record/tasks/database_tasks.rb +337 -0
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
  206. data/lib/active_record/timestamp.rb +80 -41
  207. data/lib/active_record/touch_later.rb +64 -0
  208. data/lib/active_record/transactions.rb +240 -119
  209. data/lib/active_record/translation.rb +2 -0
  210. data/lib/active_record/type/adapter_specific_registry.rb +136 -0
  211. data/lib/active_record/type/date.rb +9 -0
  212. data/lib/active_record/type/date_time.rb +9 -0
  213. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  214. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  215. data/lib/active_record/type/internal/timezone.rb +17 -0
  216. data/lib/active_record/type/json.rb +30 -0
  217. data/lib/active_record/type/serialized.rb +71 -0
  218. data/lib/active_record/type/text.rb +11 -0
  219. data/lib/active_record/type/time.rb +21 -0
  220. data/lib/active_record/type/type_map.rb +62 -0
  221. data/lib/active_record/type/unsigned_integer.rb +17 -0
  222. data/lib/active_record/type.rb +79 -0
  223. data/lib/active_record/type_caster/connection.rb +33 -0
  224. data/lib/active_record/type_caster/map.rb +23 -0
  225. data/lib/active_record/type_caster.rb +9 -0
  226. data/lib/active_record/validations/absence.rb +25 -0
  227. data/lib/active_record/validations/associated.rb +35 -18
  228. data/lib/active_record/validations/length.rb +26 -0
  229. data/lib/active_record/validations/presence.rb +68 -0
  230. data/lib/active_record/validations/uniqueness.rb +133 -75
  231. data/lib/active_record/validations.rb +53 -43
  232. data/lib/active_record/version.rb +7 -7
  233. data/lib/active_record.rb +89 -57
  234. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  235. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  236. data/lib/rails/generators/active_record/migration/migration_generator.rb +61 -8
  237. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  238. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
  239. data/lib/rails/generators/active_record/migration.rb +28 -8
  240. data/lib/rails/generators/active_record/model/model_generator.rb +23 -22
  241. data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
  242. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
  243. data/lib/rails/generators/active_record.rb +10 -16
  244. metadata +141 -62
  245. data/examples/associations.png +0 -0
  246. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  247. data/lib/active_record/associations/join_helper.rb +0 -55
  248. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  249. data/lib/active_record/associations/preloader/collection_association.rb +0 -24
  250. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  251. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  252. data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
  253. data/lib/active_record/associations/preloader/has_one.rb +0 -23
  254. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  255. data/lib/active_record/associations/preloader/singular_association.rb +0 -21
  256. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  257. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
  258. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -441
  259. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
  260. data/lib/active_record/dynamic_finder_match.rb +0 -68
  261. data/lib/active_record/dynamic_scope_match.rb +0 -23
  262. data/lib/active_record/fixtures/file.rb +0 -65
  263. data/lib/active_record/identity_map.rb +0 -162
  264. data/lib/active_record/observer.rb +0 -121
  265. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  266. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  267. data/lib/active_record/session_store.rb +0 -360
  268. data/lib/active_record/test_case.rb +0 -73
  269. data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
  270. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  271. data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
  272. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  273. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  274. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  275. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Associations
3
5
  # Association proxies in Active Record are middlemen between the object that
@@ -18,14 +20,8 @@ module ActiveRecord
18
20
  # <tt>@owner</tt>, the collection of its posts as <tt>@target</tt>, and
19
21
  # the <tt>@reflection</tt> object represents a <tt>:has_many</tt> macro.
20
22
  #
21
- # This class has most of the basic instance methods removed, and delegates
22
- # unknown methods to <tt>@target</tt> via <tt>method_missing</tt>. As a
23
- # corner case, it even removes the +class+ method and that's why you get
24
- #
25
- # blog.posts.class # => Array
26
- #
27
- # though the object behind <tt>blog.posts</tt> is not an Array, but an
28
- # ActiveRecord::Associations::HasManyAssociation.
23
+ # This class delegates unknown methods to <tt>@target</tt> via
24
+ # <tt>method_missing</tt>.
29
25
  #
30
26
  # The <tt>@target</tt> object is not \loaded until needed. For example,
31
27
  #
@@ -33,101 +29,1103 @@ module ActiveRecord
33
29
  #
34
30
  # is computed directly through SQL and does not trigger by itself the
35
31
  # instantiation of the actual post records.
36
- class CollectionProxy # :nodoc:
37
- alias :proxy_extend :extend
32
+ class CollectionProxy < Relation
33
+ def initialize(klass, association) #:nodoc:
34
+ @association = association
35
+ super klass
36
+
37
+ extensions = association.extensions
38
+ extend(*extensions) if extensions.any?
39
+ end
38
40
 
39
- instance_methods.each { |m| undef_method m unless m.to_s =~ /^(?:nil\?|send|object_id|to_a)$|^__|^respond_to|proxy_/ }
41
+ def target
42
+ @association.target
43
+ end
40
44
 
41
- delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from,
42
- :lock, :readonly, :having, :pluck, :to => :scoped
45
+ def load_target
46
+ @association.load_target
47
+ end
43
48
 
44
- delegate :target, :load_target, :loaded?, :to => :@association
49
+ # Returns +true+ if the association has been loaded, otherwise +false+.
50
+ #
51
+ # person.pets.loaded? # => false
52
+ # person.pets
53
+ # person.pets.loaded? # => true
54
+ def loaded?
55
+ @association.loaded?
56
+ end
45
57
 
46
- delegate :select, :find, :first, :last,
47
- :build, :create, :create!,
48
- :concat, :replace, :delete_all, :destroy_all, :delete, :destroy, :uniq,
49
- :sum, :count, :size, :length, :empty?,
50
- :any?, :many?, :include?,
51
- :to => :@association
58
+ ##
59
+ # :method: select
60
+ #
61
+ # :call-seq:
62
+ # select(*fields, &block)
63
+ #
64
+ # Works in two ways.
65
+ #
66
+ # *First:* Specify a subset of fields to be selected from the result set.
67
+ #
68
+ # class Person < ActiveRecord::Base
69
+ # has_many :pets
70
+ # end
71
+ #
72
+ # person.pets
73
+ # # => [
74
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
75
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
76
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
77
+ # # ]
78
+ #
79
+ # person.pets.select(:name)
80
+ # # => [
81
+ # # #<Pet id: nil, name: "Fancy-Fancy">,
82
+ # # #<Pet id: nil, name: "Spook">,
83
+ # # #<Pet id: nil, name: "Choo-Choo">
84
+ # # ]
85
+ #
86
+ # person.pets.select(:id, :name)
87
+ # # => [
88
+ # # #<Pet id: 1, name: "Fancy-Fancy">,
89
+ # # #<Pet id: 2, name: "Spook">,
90
+ # # #<Pet id: 3, name: "Choo-Choo">
91
+ # # ]
92
+ #
93
+ # Be careful because this also means you're initializing a model
94
+ # object with only the fields that you've selected. If you attempt
95
+ # to access a field except +id+ that is not in the initialized record you'll
96
+ # receive:
97
+ #
98
+ # person.pets.select(:name).first.person_id
99
+ # # => ActiveModel::MissingAttributeError: missing attribute: person_id
100
+ #
101
+ # *Second:* You can pass a block so it can be used just like Array#select.
102
+ # This builds an array of objects from the database for the scope,
103
+ # converting them into an array and iterating through them using
104
+ # Array#select.
105
+ #
106
+ # person.pets.select { |pet| pet.name =~ /oo/ }
107
+ # # => [
108
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
109
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
110
+ # # ]
52
111
 
53
- def initialize(association)
54
- @association = association
55
- Array.wrap(association.options[:extend]).each { |ext| proxy_extend(ext) }
112
+ # Finds an object in the collection responding to the +id+. Uses the same
113
+ # rules as ActiveRecord::Base.find. Returns ActiveRecord::RecordNotFound
114
+ # error if the object cannot be found.
115
+ #
116
+ # class Person < ActiveRecord::Base
117
+ # has_many :pets
118
+ # end
119
+ #
120
+ # person.pets
121
+ # # => [
122
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
123
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
124
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
125
+ # # ]
126
+ #
127
+ # person.pets.find(1) # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
128
+ # person.pets.find(4) # => ActiveRecord::RecordNotFound: Couldn't find Pet with 'id'=4
129
+ #
130
+ # person.pets.find(2) { |pet| pet.name.downcase! }
131
+ # # => #<Pet id: 2, name: "fancy-fancy", person_id: 1>
132
+ #
133
+ # person.pets.find(2, 3)
134
+ # # => [
135
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
136
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
137
+ # # ]
138
+ def find(*args)
139
+ return super if block_given?
140
+ @association.find(*args)
56
141
  end
57
142
 
143
+ ##
144
+ # :method: first
145
+ #
146
+ # :call-seq:
147
+ # first(limit = nil)
148
+ #
149
+ # Returns the first record, or the first +n+ records, from the collection.
150
+ # If the collection is empty, the first form returns +nil+, and the second
151
+ # form returns an empty array.
152
+ #
153
+ # class Person < ActiveRecord::Base
154
+ # has_many :pets
155
+ # end
156
+ #
157
+ # person.pets
158
+ # # => [
159
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
160
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
161
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
162
+ # # ]
163
+ #
164
+ # person.pets.first # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
165
+ #
166
+ # person.pets.first(2)
167
+ # # => [
168
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
169
+ # # #<Pet id: 2, name: "Spook", person_id: 1>
170
+ # # ]
171
+ #
172
+ # another_person_without.pets # => []
173
+ # another_person_without.pets.first # => nil
174
+ # another_person_without.pets.first(3) # => []
175
+
176
+ ##
177
+ # :method: second
178
+ #
179
+ # :call-seq:
180
+ # second()
181
+ #
182
+ # Same as #first except returns only the second record.
183
+
184
+ ##
185
+ # :method: third
186
+ #
187
+ # :call-seq:
188
+ # third()
189
+ #
190
+ # Same as #first except returns only the third record.
191
+
192
+ ##
193
+ # :method: fourth
194
+ #
195
+ # :call-seq:
196
+ # fourth()
197
+ #
198
+ # Same as #first except returns only the fourth record.
199
+
200
+ ##
201
+ # :method: fifth
202
+ #
203
+ # :call-seq:
204
+ # fifth()
205
+ #
206
+ # Same as #first except returns only the fifth record.
207
+
208
+ ##
209
+ # :method: forty_two
210
+ #
211
+ # :call-seq:
212
+ # forty_two()
213
+ #
214
+ # Same as #first except returns only the forty second record.
215
+ # Also known as accessing "the reddit".
216
+
217
+ ##
218
+ # :method: third_to_last
219
+ #
220
+ # :call-seq:
221
+ # third_to_last()
222
+ #
223
+ # Same as #first except returns only the third-to-last record.
224
+
225
+ ##
226
+ # :method: second_to_last
227
+ #
228
+ # :call-seq:
229
+ # second_to_last()
230
+ #
231
+ # Same as #first except returns only the second-to-last record.
232
+
233
+ # Returns the last record, or the last +n+ records, from the collection.
234
+ # If the collection is empty, the first form returns +nil+, and the second
235
+ # form returns an empty array.
236
+ #
237
+ # class Person < ActiveRecord::Base
238
+ # has_many :pets
239
+ # end
240
+ #
241
+ # person.pets
242
+ # # => [
243
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
244
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
245
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
246
+ # # ]
247
+ #
248
+ # person.pets.last # => #<Pet id: 3, name: "Choo-Choo", person_id: 1>
249
+ #
250
+ # person.pets.last(2)
251
+ # # => [
252
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
253
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
254
+ # # ]
255
+ #
256
+ # another_person_without.pets # => []
257
+ # another_person_without.pets.last # => nil
258
+ # another_person_without.pets.last(3) # => []
259
+ def last(limit = nil)
260
+ load_target if find_from_target?
261
+ super
262
+ end
263
+
264
+ # Gives a record (or N records if a parameter is supplied) from the collection
265
+ # using the same rules as <tt>ActiveRecord::Base.take</tt>.
266
+ #
267
+ # class Person < ActiveRecord::Base
268
+ # has_many :pets
269
+ # end
270
+ #
271
+ # person.pets
272
+ # # => [
273
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
274
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
275
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
276
+ # # ]
277
+ #
278
+ # person.pets.take # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
279
+ #
280
+ # person.pets.take(2)
281
+ # # => [
282
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
283
+ # # #<Pet id: 2, name: "Spook", person_id: 1>
284
+ # # ]
285
+ #
286
+ # another_person_without.pets # => []
287
+ # another_person_without.pets.take # => nil
288
+ # another_person_without.pets.take(2) # => []
289
+ def take(limit = nil)
290
+ load_target if find_from_target?
291
+ super
292
+ end
293
+
294
+ # Returns a new object of the collection type that has been instantiated
295
+ # with +attributes+ and linked to this object, but have not yet been saved.
296
+ # You can pass an array of attributes hashes, this will return an array
297
+ # with the new objects.
298
+ #
299
+ # class Person
300
+ # has_many :pets
301
+ # end
302
+ #
303
+ # person.pets.build
304
+ # # => #<Pet id: nil, name: nil, person_id: 1>
305
+ #
306
+ # person.pets.build(name: 'Fancy-Fancy')
307
+ # # => #<Pet id: nil, name: "Fancy-Fancy", person_id: 1>
308
+ #
309
+ # person.pets.build([{name: 'Spook'}, {name: 'Choo-Choo'}, {name: 'Brain'}])
310
+ # # => [
311
+ # # #<Pet id: nil, name: "Spook", person_id: 1>,
312
+ # # #<Pet id: nil, name: "Choo-Choo", person_id: 1>,
313
+ # # #<Pet id: nil, name: "Brain", person_id: 1>
314
+ # # ]
315
+ #
316
+ # person.pets.size # => 5 # size of the collection
317
+ # person.pets.count # => 0 # count from database
318
+ def build(attributes = {}, &block)
319
+ @association.build(attributes, &block)
320
+ end
58
321
  alias_method :new, :build
59
322
 
323
+ # Returns a new object of the collection type that has been instantiated with
324
+ # attributes, linked to this object and that has already been saved (if it
325
+ # passes the validations).
326
+ #
327
+ # class Person
328
+ # has_many :pets
329
+ # end
330
+ #
331
+ # person.pets.create(name: 'Fancy-Fancy')
332
+ # # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
333
+ #
334
+ # person.pets.create([{name: 'Spook'}, {name: 'Choo-Choo'}])
335
+ # # => [
336
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
337
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
338
+ # # ]
339
+ #
340
+ # person.pets.size # => 3
341
+ # person.pets.count # => 3
342
+ #
343
+ # person.pets.find(1, 2, 3)
344
+ # # => [
345
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
346
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
347
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
348
+ # # ]
349
+ def create(attributes = {}, &block)
350
+ @association.create(attributes, &block)
351
+ end
352
+
353
+ # Like #create, except that if the record is invalid, raises an exception.
354
+ #
355
+ # class Person
356
+ # has_many :pets
357
+ # end
358
+ #
359
+ # class Pet
360
+ # validates :name, presence: true
361
+ # end
362
+ #
363
+ # person.pets.create!(name: nil)
364
+ # # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
365
+ def create!(attributes = {}, &block)
366
+ @association.create!(attributes, &block)
367
+ end
368
+
369
+ # Replaces this collection with +other_array+. This will perform a diff
370
+ # and delete/add only records that have changed.
371
+ #
372
+ # class Person < ActiveRecord::Base
373
+ # has_many :pets
374
+ # end
375
+ #
376
+ # person.pets
377
+ # # => [#<Pet id: 1, name: "Gorby", group: "cats", person_id: 1>]
378
+ #
379
+ # other_pets = [Pet.new(name: 'Puff', group: 'celebrities']
380
+ #
381
+ # person.pets.replace(other_pets)
382
+ #
383
+ # person.pets
384
+ # # => [#<Pet id: 2, name: "Puff", group: "celebrities", person_id: 1>]
385
+ #
386
+ # If the supplied array has an incorrect association type, it raises
387
+ # an <tt>ActiveRecord::AssociationTypeMismatch</tt> error:
388
+ #
389
+ # person.pets.replace(["doo", "ggie", "gaga"])
390
+ # # => ActiveRecord::AssociationTypeMismatch: Pet expected, got String
391
+ def replace(other_array)
392
+ @association.replace(other_array)
393
+ end
394
+
395
+ # Deletes all the records from the collection according to the strategy
396
+ # specified by the +:dependent+ option. If no +:dependent+ option is given,
397
+ # then it will follow the default strategy.
398
+ #
399
+ # For <tt>has_many :through</tt> associations, the default deletion strategy is
400
+ # +:delete_all+.
401
+ #
402
+ # For +has_many+ associations, the default deletion strategy is +:nullify+.
403
+ # This sets the foreign keys to +NULL+.
404
+ #
405
+ # class Person < ActiveRecord::Base
406
+ # has_many :pets # dependent: :nullify option by default
407
+ # end
408
+ #
409
+ # person.pets.size # => 3
410
+ # person.pets
411
+ # # => [
412
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
413
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
414
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
415
+ # # ]
416
+ #
417
+ # person.pets.delete_all
418
+ # # => [
419
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
420
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
421
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
422
+ # # ]
423
+ #
424
+ # person.pets.size # => 0
425
+ # person.pets # => []
426
+ #
427
+ # Pet.find(1, 2, 3)
428
+ # # => [
429
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: nil>,
430
+ # # #<Pet id: 2, name: "Spook", person_id: nil>,
431
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: nil>
432
+ # # ]
433
+ #
434
+ # Both +has_many+ and <tt>has_many :through</tt> dependencies default to the
435
+ # +:delete_all+ strategy if the +:dependent+ option is set to +:destroy+.
436
+ # Records are not instantiated and callbacks will not be fired.
437
+ #
438
+ # class Person < ActiveRecord::Base
439
+ # has_many :pets, dependent: :destroy
440
+ # end
441
+ #
442
+ # person.pets.size # => 3
443
+ # person.pets
444
+ # # => [
445
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
446
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
447
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
448
+ # # ]
449
+ #
450
+ # person.pets.delete_all
451
+ #
452
+ # Pet.find(1, 2, 3)
453
+ # # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)
454
+ #
455
+ # If it is set to <tt>:delete_all</tt>, all the objects are deleted
456
+ # *without* calling their +destroy+ method.
457
+ #
458
+ # class Person < ActiveRecord::Base
459
+ # has_many :pets, dependent: :delete_all
460
+ # end
461
+ #
462
+ # person.pets.size # => 3
463
+ # person.pets
464
+ # # => [
465
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
466
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
467
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
468
+ # # ]
469
+ #
470
+ # person.pets.delete_all
471
+ #
472
+ # Pet.find(1, 2, 3)
473
+ # # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)
474
+ def delete_all(dependent = nil)
475
+ @association.delete_all(dependent).tap { reset_scope }
476
+ end
477
+
478
+ # Deletes the records of the collection directly from the database
479
+ # ignoring the +:dependent+ option. Records are instantiated and it
480
+ # invokes +before_remove+, +after_remove+ , +before_destroy+ and
481
+ # +after_destroy+ callbacks.
482
+ #
483
+ # class Person < ActiveRecord::Base
484
+ # has_many :pets
485
+ # end
486
+ #
487
+ # person.pets.size # => 3
488
+ # person.pets
489
+ # # => [
490
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
491
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
492
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
493
+ # # ]
494
+ #
495
+ # person.pets.destroy_all
496
+ #
497
+ # person.pets.size # => 0
498
+ # person.pets # => []
499
+ #
500
+ # Pet.find(1) # => Couldn't find Pet with id=1
501
+ def destroy_all
502
+ @association.destroy_all.tap { reset_scope }
503
+ end
504
+
505
+ # Deletes the +records+ supplied from the collection according to the strategy
506
+ # specified by the +:dependent+ option. If no +:dependent+ option is given,
507
+ # then it will follow the default strategy. Returns an array with the
508
+ # deleted records.
509
+ #
510
+ # For <tt>has_many :through</tt> associations, the default deletion strategy is
511
+ # +:delete_all+.
512
+ #
513
+ # For +has_many+ associations, the default deletion strategy is +:nullify+.
514
+ # This sets the foreign keys to +NULL+.
515
+ #
516
+ # class Person < ActiveRecord::Base
517
+ # has_many :pets # dependent: :nullify option by default
518
+ # end
519
+ #
520
+ # person.pets.size # => 3
521
+ # person.pets
522
+ # # => [
523
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
524
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
525
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
526
+ # # ]
527
+ #
528
+ # person.pets.delete(Pet.find(1))
529
+ # # => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]
530
+ #
531
+ # person.pets.size # => 2
532
+ # person.pets
533
+ # # => [
534
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
535
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
536
+ # # ]
537
+ #
538
+ # Pet.find(1)
539
+ # # => #<Pet id: 1, name: "Fancy-Fancy", person_id: nil>
540
+ #
541
+ # If it is set to <tt>:destroy</tt> all the +records+ are removed by calling
542
+ # their +destroy+ method. See +destroy+ for more information.
543
+ #
544
+ # class Person < ActiveRecord::Base
545
+ # has_many :pets, dependent: :destroy
546
+ # end
547
+ #
548
+ # person.pets.size # => 3
549
+ # person.pets
550
+ # # => [
551
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
552
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
553
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
554
+ # # ]
555
+ #
556
+ # person.pets.delete(Pet.find(1), Pet.find(3))
557
+ # # => [
558
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
559
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
560
+ # # ]
561
+ #
562
+ # person.pets.size # => 1
563
+ # person.pets
564
+ # # => [#<Pet id: 2, name: "Spook", person_id: 1>]
565
+ #
566
+ # Pet.find(1, 3)
567
+ # # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 3)
568
+ #
569
+ # If it is set to <tt>:delete_all</tt>, all the +records+ are deleted
570
+ # *without* calling their +destroy+ method.
571
+ #
572
+ # class Person < ActiveRecord::Base
573
+ # has_many :pets, dependent: :delete_all
574
+ # end
575
+ #
576
+ # person.pets.size # => 3
577
+ # person.pets
578
+ # # => [
579
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
580
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
581
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
582
+ # # ]
583
+ #
584
+ # person.pets.delete(Pet.find(1))
585
+ # # => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]
586
+ #
587
+ # person.pets.size # => 2
588
+ # person.pets
589
+ # # => [
590
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
591
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
592
+ # # ]
593
+ #
594
+ # Pet.find(1)
595
+ # # => ActiveRecord::RecordNotFound: Couldn't find Pet with 'id'=1
596
+ #
597
+ # You can pass +Integer+ or +String+ values, it finds the records
598
+ # responding to the +id+ and executes delete on them.
599
+ #
600
+ # class Person < ActiveRecord::Base
601
+ # has_many :pets
602
+ # end
603
+ #
604
+ # person.pets.size # => 3
605
+ # person.pets
606
+ # # => [
607
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
608
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
609
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
610
+ # # ]
611
+ #
612
+ # person.pets.delete("1")
613
+ # # => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]
614
+ #
615
+ # person.pets.delete(2, 3)
616
+ # # => [
617
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
618
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
619
+ # # ]
620
+ def delete(*records)
621
+ @association.delete(*records).tap { reset_scope }
622
+ end
623
+
624
+ # Destroys the +records+ supplied and removes them from the collection.
625
+ # This method will _always_ remove record from the database ignoring
626
+ # the +:dependent+ option. Returns an array with the removed records.
627
+ #
628
+ # class Person < ActiveRecord::Base
629
+ # has_many :pets
630
+ # end
631
+ #
632
+ # person.pets.size # => 3
633
+ # person.pets
634
+ # # => [
635
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
636
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
637
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
638
+ # # ]
639
+ #
640
+ # person.pets.destroy(Pet.find(1))
641
+ # # => [#<Pet id: 1, name: "Fancy-Fancy", person_id: 1>]
642
+ #
643
+ # person.pets.size # => 2
644
+ # person.pets
645
+ # # => [
646
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
647
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
648
+ # # ]
649
+ #
650
+ # person.pets.destroy(Pet.find(2), Pet.find(3))
651
+ # # => [
652
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
653
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
654
+ # # ]
655
+ #
656
+ # person.pets.size # => 0
657
+ # person.pets # => []
658
+ #
659
+ # Pet.find(1, 2, 3) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (1, 2, 3)
660
+ #
661
+ # You can pass +Integer+ or +String+ values, it finds the records
662
+ # responding to the +id+ and then deletes them from the database.
663
+ #
664
+ # person.pets.size # => 3
665
+ # person.pets
666
+ # # => [
667
+ # # #<Pet id: 4, name: "Benny", person_id: 1>,
668
+ # # #<Pet id: 5, name: "Brain", person_id: 1>,
669
+ # # #<Pet id: 6, name: "Boss", person_id: 1>
670
+ # # ]
671
+ #
672
+ # person.pets.destroy("4")
673
+ # # => #<Pet id: 4, name: "Benny", person_id: 1>
674
+ #
675
+ # person.pets.size # => 2
676
+ # person.pets
677
+ # # => [
678
+ # # #<Pet id: 5, name: "Brain", person_id: 1>,
679
+ # # #<Pet id: 6, name: "Boss", person_id: 1>
680
+ # # ]
681
+ #
682
+ # person.pets.destroy(5, 6)
683
+ # # => [
684
+ # # #<Pet id: 5, name: "Brain", person_id: 1>,
685
+ # # #<Pet id: 6, name: "Boss", person_id: 1>
686
+ # # ]
687
+ #
688
+ # person.pets.size # => 0
689
+ # person.pets # => []
690
+ #
691
+ # Pet.find(4, 5, 6) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with 'id': (4, 5, 6)
692
+ def destroy(*records)
693
+ @association.destroy(*records).tap { reset_scope }
694
+ end
695
+
696
+ ##
697
+ # :method: distinct
698
+ #
699
+ # :call-seq:
700
+ # distinct(value = true)
701
+ #
702
+ # Specifies whether the records should be unique or not.
703
+ #
704
+ # class Person < ActiveRecord::Base
705
+ # has_many :pets
706
+ # end
707
+ #
708
+ # person.pets.select(:name)
709
+ # # => [
710
+ # # #<Pet name: "Fancy-Fancy">,
711
+ # # #<Pet name: "Fancy-Fancy">
712
+ # # ]
713
+ #
714
+ # person.pets.select(:name).distinct
715
+ # # => [#<Pet name: "Fancy-Fancy">]
716
+ #
717
+ # person.pets.select(:name).distinct.distinct(false)
718
+ # # => [
719
+ # # #<Pet name: "Fancy-Fancy">,
720
+ # # #<Pet name: "Fancy-Fancy">
721
+ # # ]
722
+
723
+ #--
724
+ def calculate(operation, column_name)
725
+ null_scope? ? scope.calculate(operation, column_name) : super
726
+ end
727
+
728
+ def pluck(*column_names)
729
+ null_scope? ? scope.pluck(*column_names) : super
730
+ end
731
+
732
+ ##
733
+ # :method: count
734
+ #
735
+ # :call-seq:
736
+ # count(column_name = nil, &block)
737
+ #
738
+ # Count all records.
739
+ #
740
+ # class Person < ActiveRecord::Base
741
+ # has_many :pets
742
+ # end
743
+ #
744
+ # # This will perform the count using SQL.
745
+ # person.pets.count # => 3
746
+ # person.pets
747
+ # # => [
748
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
749
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
750
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
751
+ # # ]
752
+ #
753
+ # Passing a block will select all of a person's pets in SQL and then
754
+ # perform the count using Ruby.
755
+ #
756
+ # person.pets.count { |pet| pet.name.include?('-') } # => 2
757
+
758
+ # Returns the size of the collection. If the collection hasn't been loaded,
759
+ # it executes a <tt>SELECT COUNT(*)</tt> query. Else it calls <tt>collection.size</tt>.
760
+ #
761
+ # If the collection has been already loaded +size+ and +length+ are
762
+ # equivalent. If not and you are going to need the records anyway
763
+ # +length+ will take one less query. Otherwise +size+ is more efficient.
764
+ #
765
+ # class Person < ActiveRecord::Base
766
+ # has_many :pets
767
+ # end
768
+ #
769
+ # person.pets.size # => 3
770
+ # # executes something like SELECT COUNT(*) FROM "pets" WHERE "pets"."person_id" = 1
771
+ #
772
+ # person.pets # This will execute a SELECT * FROM query
773
+ # # => [
774
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
775
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
776
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
777
+ # # ]
778
+ #
779
+ # person.pets.size # => 3
780
+ # # Because the collection is already loaded, this will behave like
781
+ # # collection.size and no SQL count query is executed.
782
+ def size
783
+ @association.size
784
+ end
785
+
786
+ ##
787
+ # :method: length
788
+ #
789
+ # :call-seq:
790
+ # length()
791
+ #
792
+ # Returns the size of the collection calling +size+ on the target.
793
+ # If the collection has been already loaded, +length+ and +size+ are
794
+ # equivalent. If not and you are going to need the records anyway this
795
+ # method will take one less query. Otherwise +size+ is more efficient.
796
+ #
797
+ # class Person < ActiveRecord::Base
798
+ # has_many :pets
799
+ # end
800
+ #
801
+ # person.pets.length # => 3
802
+ # # executes something like SELECT "pets".* FROM "pets" WHERE "pets"."person_id" = 1
803
+ #
804
+ # # Because the collection is loaded, you can
805
+ # # call the collection with no additional queries:
806
+ # person.pets
807
+ # # => [
808
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
809
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
810
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
811
+ # # ]
812
+
813
+ # Returns +true+ if the collection is empty. If the collection has been
814
+ # loaded it is equivalent
815
+ # to <tt>collection.size.zero?</tt>. If the collection has not been loaded,
816
+ # it is equivalent to <tt>!collection.exists?</tt>. If the collection has
817
+ # not already been loaded and you are going to fetch the records anyway it
818
+ # is better to check <tt>collection.length.zero?</tt>.
819
+ #
820
+ # class Person < ActiveRecord::Base
821
+ # has_many :pets
822
+ # end
823
+ #
824
+ # person.pets.count # => 1
825
+ # person.pets.empty? # => false
826
+ #
827
+ # person.pets.delete_all
828
+ #
829
+ # person.pets.count # => 0
830
+ # person.pets.empty? # => true
831
+ def empty?
832
+ @association.empty?
833
+ end
834
+
835
+ ##
836
+ # :method: any?
837
+ #
838
+ # :call-seq:
839
+ # any?()
840
+ #
841
+ # Returns +true+ if the collection is not empty.
842
+ #
843
+ # class Person < ActiveRecord::Base
844
+ # has_many :pets
845
+ # end
846
+ #
847
+ # person.pets.count # => 0
848
+ # person.pets.any? # => false
849
+ #
850
+ # person.pets << Pet.new(name: 'Snoop')
851
+ # person.pets.count # => 1
852
+ # person.pets.any? # => true
853
+ #
854
+ # You can also pass a +block+ to define criteria. The behavior
855
+ # is the same, it returns true if the collection based on the
856
+ # criteria is not empty.
857
+ #
858
+ # person.pets
859
+ # # => [#<Pet name: "Snoop", group: "dogs">]
860
+ #
861
+ # person.pets.any? do |pet|
862
+ # pet.group == 'cats'
863
+ # end
864
+ # # => false
865
+ #
866
+ # person.pets.any? do |pet|
867
+ # pet.group == 'dogs'
868
+ # end
869
+ # # => true
870
+
871
+ ##
872
+ # :method: many?
873
+ #
874
+ # :call-seq:
875
+ # many?()
876
+ #
877
+ # Returns true if the collection has more than one record.
878
+ # Equivalent to <tt>collection.size > 1</tt>.
879
+ #
880
+ # class Person < ActiveRecord::Base
881
+ # has_many :pets
882
+ # end
883
+ #
884
+ # person.pets.count # => 1
885
+ # person.pets.many? # => false
886
+ #
887
+ # person.pets << Pet.new(name: 'Snoopy')
888
+ # person.pets.count # => 2
889
+ # person.pets.many? # => true
890
+ #
891
+ # You can also pass a +block+ to define criteria. The
892
+ # behavior is the same, it returns true if the collection
893
+ # based on the criteria has more than one record.
894
+ #
895
+ # person.pets
896
+ # # => [
897
+ # # #<Pet name: "Gorby", group: "cats">,
898
+ # # #<Pet name: "Puff", group: "cats">,
899
+ # # #<Pet name: "Snoop", group: "dogs">
900
+ # # ]
901
+ #
902
+ # person.pets.many? do |pet|
903
+ # pet.group == 'dogs'
904
+ # end
905
+ # # => false
906
+ #
907
+ # person.pets.many? do |pet|
908
+ # pet.group == 'cats'
909
+ # end
910
+ # # => true
911
+
912
+ # Returns +true+ if the given +record+ is present in the collection.
913
+ #
914
+ # class Person < ActiveRecord::Base
915
+ # has_many :pets
916
+ # end
917
+ #
918
+ # person.pets # => [#<Pet id: 20, name: "Snoop">]
919
+ #
920
+ # person.pets.include?(Pet.find(20)) # => true
921
+ # person.pets.include?(Pet.find(21)) # => false
922
+ def include?(record)
923
+ !!@association.include?(record)
924
+ end
925
+
60
926
  def proxy_association
61
927
  @association
62
928
  end
63
929
 
64
- def scoped
65
- association = @association
66
- association.scoped.extending do
67
- define_method(:proxy_association) { association }
68
- end
930
+ # Returns a <tt>Relation</tt> object for the records in this association
931
+ def scope
932
+ @scope ||= @association.scope
69
933
  end
70
934
 
71
- def respond_to?(name, include_private = false)
72
- super ||
73
- (load_target && target.respond_to?(name, include_private)) ||
74
- proxy_association.klass.respond_to?(name, include_private)
75
- end
76
-
77
- def method_missing(method, *args, &block)
78
- match = DynamicFinderMatch.match(method)
79
- if match && match.instantiator?
80
- send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |record|
81
- proxy_association.send :set_owner_attributes, record
82
- proxy_association.send :add_to_target, record
83
- yield(record) if block_given?
84
- end.tap do |record|
85
- proxy_association.send :set_inverse_instance, record
86
- end
87
-
88
- elsif target.respond_to?(method) || (!proxy_association.klass.respond_to?(method) && Class.respond_to?(method))
89
- if load_target
90
- if target.respond_to?(method)
91
- target.send(method, *args, &block)
92
- else
93
- begin
94
- super
95
- rescue NoMethodError => e
96
- raise e, e.message.sub(/ for #<.*$/, " via proxy for #{target}")
97
- end
98
- end
99
- end
100
-
101
- else
102
- scoped.readonly(nil).send(method, *args, &block)
103
- end
935
+ # Equivalent to <tt>Array#==</tt>. Returns +true+ if the two arrays
936
+ # contain the same number of elements and if each element is equal
937
+ # to the corresponding element in the +other+ array, otherwise returns
938
+ # +false+.
939
+ #
940
+ # class Person < ActiveRecord::Base
941
+ # has_many :pets
942
+ # end
943
+ #
944
+ # person.pets
945
+ # # => [
946
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
947
+ # # #<Pet id: 2, name: "Spook", person_id: 1>
948
+ # # ]
949
+ #
950
+ # other = person.pets.to_ary
951
+ #
952
+ # person.pets == other
953
+ # # => true
954
+ #
955
+ # other = [Pet.new(id: 1), Pet.new(id: 2)]
956
+ #
957
+ # person.pets == other
958
+ # # => false
959
+ def ==(other)
960
+ load_target == other
104
961
  end
105
962
 
106
- # Forwards <tt>===</tt> explicitly to the \target because the instance method
107
- # removal above doesn't catch it. Loads the \target if needed.
108
- def ===(other)
109
- other === load_target
110
- end
963
+ ##
964
+ # :method: to_ary
965
+ #
966
+ # :call-seq:
967
+ # to_ary()
968
+ #
969
+ # Returns a new array of objects from the collection. If the collection
970
+ # hasn't been loaded, it fetches the records from the database.
971
+ #
972
+ # class Person < ActiveRecord::Base
973
+ # has_many :pets
974
+ # end
975
+ #
976
+ # person.pets
977
+ # # => [
978
+ # # #<Pet id: 4, name: "Benny", person_id: 1>,
979
+ # # #<Pet id: 5, name: "Brain", person_id: 1>,
980
+ # # #<Pet id: 6, name: "Boss", person_id: 1>
981
+ # # ]
982
+ #
983
+ # other_pets = person.pets.to_ary
984
+ # # => [
985
+ # # #<Pet id: 4, name: "Benny", person_id: 1>,
986
+ # # #<Pet id: 5, name: "Brain", person_id: 1>,
987
+ # # #<Pet id: 6, name: "Boss", person_id: 1>
988
+ # # ]
989
+ #
990
+ # other_pets.replace([Pet.new(name: 'BooGoo')])
991
+ #
992
+ # other_pets
993
+ # # => [#<Pet id: nil, name: "BooGoo", person_id: 1>]
994
+ #
995
+ # person.pets
996
+ # # This is not affected by replace
997
+ # # => [
998
+ # # #<Pet id: 4, name: "Benny", person_id: 1>,
999
+ # # #<Pet id: 5, name: "Brain", person_id: 1>,
1000
+ # # #<Pet id: 6, name: "Boss", person_id: 1>
1001
+ # # ]
111
1002
 
112
- def to_ary
113
- load_target.dup
1003
+ def records # :nodoc:
1004
+ load_target
114
1005
  end
115
- alias_method :to_a, :to_ary
116
1006
 
1007
+ # Adds one or more +records+ to the collection by setting their foreign keys
1008
+ # to the association's primary key. Since +<<+ flattens its argument list and
1009
+ # inserts each record, +push+ and +concat+ behave identically. Returns +self+
1010
+ # so several appends may be chained together.
1011
+ #
1012
+ # class Person < ActiveRecord::Base
1013
+ # has_many :pets
1014
+ # end
1015
+ #
1016
+ # person.pets.size # => 0
1017
+ # person.pets << Pet.new(name: 'Fancy-Fancy')
1018
+ # person.pets << [Pet.new(name: 'Spook'), Pet.new(name: 'Choo-Choo')]
1019
+ # person.pets.size # => 3
1020
+ #
1021
+ # person.id # => 1
1022
+ # person.pets
1023
+ # # => [
1024
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
1025
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
1026
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
1027
+ # # ]
117
1028
  def <<(*records)
118
1029
  proxy_association.concat(records) && self
119
1030
  end
120
1031
  alias_method :push, :<<
1032
+ alias_method :append, :<<
1033
+ alias_method :concat, :<<
121
1034
 
1035
+ def prepend(*args)
1036
+ raise NoMethodError, "prepend on association is not defined. Please use <<, push or append"
1037
+ end
1038
+
1039
+ # Equivalent to +delete_all+. The difference is that returns +self+, instead
1040
+ # of an array with the deleted objects, so methods can be chained. See
1041
+ # +delete_all+ for more information.
1042
+ # Note that because +delete_all+ removes records by directly
1043
+ # running an SQL query into the database, the +updated_at+ column of
1044
+ # the object is not changed.
122
1045
  def clear
123
1046
  delete_all
124
1047
  self
125
1048
  end
126
1049
 
1050
+ # Reloads the collection from the database. Returns +self+.
1051
+ #
1052
+ # class Person < ActiveRecord::Base
1053
+ # has_many :pets
1054
+ # end
1055
+ #
1056
+ # person.pets # fetches pets from the database
1057
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
1058
+ #
1059
+ # person.pets # uses the pets cache
1060
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
1061
+ #
1062
+ # person.pets.reload # fetches pets from the database
1063
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
127
1064
  def reload
128
1065
  proxy_association.reload
1066
+ reset_scope
1067
+ end
1068
+
1069
+ # Unloads the association. Returns +self+.
1070
+ #
1071
+ # class Person < ActiveRecord::Base
1072
+ # has_many :pets
1073
+ # end
1074
+ #
1075
+ # person.pets # fetches pets from the database
1076
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
1077
+ #
1078
+ # person.pets # uses the pets cache
1079
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
1080
+ #
1081
+ # person.pets.reset # clears the pets cache
1082
+ #
1083
+ # person.pets # fetches pets from the database
1084
+ # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>]
1085
+ def reset
1086
+ proxy_association.reset
1087
+ proxy_association.reset_scope
1088
+ reset_scope
1089
+ end
1090
+
1091
+ def reset_scope # :nodoc:
1092
+ @offsets = {}
1093
+ @scope = nil
129
1094
  self
130
1095
  end
1096
+
1097
+ delegate_methods = [
1098
+ QueryMethods,
1099
+ SpawnMethods,
1100
+ ].flat_map { |klass|
1101
+ klass.public_instance_methods(false)
1102
+ } - self.public_instance_methods(false) - [:select] + [:scoping]
1103
+
1104
+ delegate(*delegate_methods, to: :scope)
1105
+
1106
+ private
1107
+
1108
+ def find_nth_with_limit(index, limit)
1109
+ load_target if find_from_target?
1110
+ super
1111
+ end
1112
+
1113
+ def find_nth_from_last(index)
1114
+ load_target if find_from_target?
1115
+ super
1116
+ end
1117
+
1118
+ def null_scope?
1119
+ @association.null_scope?
1120
+ end
1121
+
1122
+ def find_from_target?
1123
+ @association.find_from_target?
1124
+ end
1125
+
1126
+ def exec_queries
1127
+ load_target
1128
+ end
131
1129
  end
132
1130
  end
133
1131
  end