activerecord 5.2.3

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

Potentially problematic release.


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

Files changed (244) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +937 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +217 -0
  5. data/examples/performance.rb +185 -0
  6. data/examples/simple.rb +15 -0
  7. data/lib/active_record.rb +188 -0
  8. data/lib/active_record/aggregations.rb +283 -0
  9. data/lib/active_record/association_relation.rb +40 -0
  10. data/lib/active_record/associations.rb +1860 -0
  11. data/lib/active_record/associations/alias_tracker.rb +81 -0
  12. data/lib/active_record/associations/association.rb +299 -0
  13. data/lib/active_record/associations/association_scope.rb +168 -0
  14. data/lib/active_record/associations/belongs_to_association.rb +130 -0
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
  16. data/lib/active_record/associations/builder/association.rb +140 -0
  17. data/lib/active_record/associations/builder/belongs_to.rb +163 -0
  18. data/lib/active_record/associations/builder/collection_association.rb +82 -0
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +135 -0
  20. data/lib/active_record/associations/builder/has_many.rb +17 -0
  21. data/lib/active_record/associations/builder/has_one.rb +30 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +42 -0
  23. data/lib/active_record/associations/collection_association.rb +513 -0
  24. data/lib/active_record/associations/collection_proxy.rb +1131 -0
  25. data/lib/active_record/associations/foreign_association.rb +13 -0
  26. data/lib/active_record/associations/has_many_association.rb +144 -0
  27. data/lib/active_record/associations/has_many_through_association.rb +227 -0
  28. data/lib/active_record/associations/has_one_association.rb +120 -0
  29. data/lib/active_record/associations/has_one_through_association.rb +45 -0
  30. data/lib/active_record/associations/join_dependency.rb +262 -0
  31. data/lib/active_record/associations/join_dependency/join_association.rb +60 -0
  32. data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
  33. data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
  34. data/lib/active_record/associations/preloader.rb +193 -0
  35. data/lib/active_record/associations/preloader/association.rb +131 -0
  36. data/lib/active_record/associations/preloader/through_association.rb +107 -0
  37. data/lib/active_record/associations/singular_association.rb +73 -0
  38. data/lib/active_record/associations/through_association.rb +121 -0
  39. data/lib/active_record/attribute_assignment.rb +88 -0
  40. data/lib/active_record/attribute_decorators.rb +90 -0
  41. data/lib/active_record/attribute_methods.rb +492 -0
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +78 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +150 -0
  44. data/lib/active_record/attribute_methods/primary_key.rb +143 -0
  45. data/lib/active_record/attribute_methods/query.rb +42 -0
  46. data/lib/active_record/attribute_methods/read.rb +85 -0
  47. data/lib/active_record/attribute_methods/serialization.rb +90 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
  49. data/lib/active_record/attribute_methods/write.rb +68 -0
  50. data/lib/active_record/attributes.rb +266 -0
  51. data/lib/active_record/autosave_association.rb +498 -0
  52. data/lib/active_record/base.rb +329 -0
  53. data/lib/active_record/callbacks.rb +353 -0
  54. data/lib/active_record/coders/json.rb +15 -0
  55. data/lib/active_record/coders/yaml_column.rb +50 -0
  56. data/lib/active_record/collection_cache_key.rb +53 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1068 -0
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +72 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +540 -0
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +145 -0
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +200 -0
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +685 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1396 -0
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +628 -0
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +887 -0
  70. data/lib/active_record/connection_adapters/column.rb +91 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
  73. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -0
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +863 -0
  118. data/lib/active_record/connection_adapters/schema_cache.rb +118 -0
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +573 -0
  127. data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
  128. data/lib/active_record/connection_handling.rb +145 -0
  129. data/lib/active_record/core.rb +559 -0
  130. data/lib/active_record/counter_cache.rb +218 -0
  131. data/lib/active_record/define_callbacks.rb +22 -0
  132. data/lib/active_record/dynamic_matchers.rb +122 -0
  133. data/lib/active_record/enum.rb +244 -0
  134. data/lib/active_record/errors.rb +380 -0
  135. data/lib/active_record/explain.rb +50 -0
  136. data/lib/active_record/explain_registry.rb +32 -0
  137. data/lib/active_record/explain_subscriber.rb +34 -0
  138. data/lib/active_record/fixture_set/file.rb +82 -0
  139. data/lib/active_record/fixtures.rb +1065 -0
  140. data/lib/active_record/gem_version.rb +17 -0
  141. data/lib/active_record/inheritance.rb +283 -0
  142. data/lib/active_record/integration.rb +155 -0
  143. data/lib/active_record/internal_metadata.rb +45 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  145. data/lib/active_record/locale/en.yml +48 -0
  146. data/lib/active_record/locking/optimistic.rb +198 -0
  147. data/lib/active_record/locking/pessimistic.rb +89 -0
  148. data/lib/active_record/log_subscriber.rb +137 -0
  149. data/lib/active_record/migration.rb +1378 -0
  150. data/lib/active_record/migration/command_recorder.rb +240 -0
  151. data/lib/active_record/migration/compatibility.rb +217 -0
  152. data/lib/active_record/migration/join_table.rb +17 -0
  153. data/lib/active_record/model_schema.rb +521 -0
  154. data/lib/active_record/nested_attributes.rb +600 -0
  155. data/lib/active_record/no_touching.rb +58 -0
  156. data/lib/active_record/null_relation.rb +68 -0
  157. data/lib/active_record/persistence.rb +763 -0
  158. data/lib/active_record/query_cache.rb +45 -0
  159. data/lib/active_record/querying.rb +70 -0
  160. data/lib/active_record/railtie.rb +226 -0
  161. data/lib/active_record/railties/console_sandbox.rb +7 -0
  162. data/lib/active_record/railties/controller_runtime.rb +56 -0
  163. data/lib/active_record/railties/databases.rake +377 -0
  164. data/lib/active_record/readonly_attributes.rb +24 -0
  165. data/lib/active_record/reflection.rb +1044 -0
  166. data/lib/active_record/relation.rb +629 -0
  167. data/lib/active_record/relation/batches.rb +287 -0
  168. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  169. data/lib/active_record/relation/calculations.rb +417 -0
  170. data/lib/active_record/relation/delegation.rb +147 -0
  171. data/lib/active_record/relation/finder_methods.rb +565 -0
  172. data/lib/active_record/relation/from_clause.rb +26 -0
  173. data/lib/active_record/relation/merger.rb +193 -0
  174. data/lib/active_record/relation/predicate_builder.rb +152 -0
  175. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  176. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  177. data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
  178. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
  179. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  180. data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
  181. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  182. data/lib/active_record/relation/query_attribute.rb +45 -0
  183. data/lib/active_record/relation/query_methods.rb +1231 -0
  184. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  185. data/lib/active_record/relation/spawn_methods.rb +77 -0
  186. data/lib/active_record/relation/where_clause.rb +186 -0
  187. data/lib/active_record/relation/where_clause_factory.rb +34 -0
  188. data/lib/active_record/result.rb +149 -0
  189. data/lib/active_record/runtime_registry.rb +24 -0
  190. data/lib/active_record/sanitization.rb +222 -0
  191. data/lib/active_record/schema.rb +70 -0
  192. data/lib/active_record/schema_dumper.rb +255 -0
  193. data/lib/active_record/schema_migration.rb +56 -0
  194. data/lib/active_record/scoping.rb +106 -0
  195. data/lib/active_record/scoping/default.rb +152 -0
  196. data/lib/active_record/scoping/named.rb +213 -0
  197. data/lib/active_record/secure_token.rb +40 -0
  198. data/lib/active_record/serialization.rb +22 -0
  199. data/lib/active_record/statement_cache.rb +121 -0
  200. data/lib/active_record/store.rb +211 -0
  201. data/lib/active_record/suppressor.rb +61 -0
  202. data/lib/active_record/table_metadata.rb +82 -0
  203. data/lib/active_record/tasks/database_tasks.rb +337 -0
  204. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  205. data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
  206. data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
  207. data/lib/active_record/timestamp.rb +153 -0
  208. data/lib/active_record/touch_later.rb +64 -0
  209. data/lib/active_record/transactions.rb +502 -0
  210. data/lib/active_record/translation.rb +24 -0
  211. data/lib/active_record/type.rb +79 -0
  212. data/lib/active_record/type/adapter_specific_registry.rb +136 -0
  213. data/lib/active_record/type/date.rb +9 -0
  214. data/lib/active_record/type/date_time.rb +9 -0
  215. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  216. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  217. data/lib/active_record/type/internal/timezone.rb +17 -0
  218. data/lib/active_record/type/json.rb +30 -0
  219. data/lib/active_record/type/serialized.rb +71 -0
  220. data/lib/active_record/type/text.rb +11 -0
  221. data/lib/active_record/type/time.rb +21 -0
  222. data/lib/active_record/type/type_map.rb +62 -0
  223. data/lib/active_record/type/unsigned_integer.rb +17 -0
  224. data/lib/active_record/type_caster.rb +9 -0
  225. data/lib/active_record/type_caster/connection.rb +33 -0
  226. data/lib/active_record/type_caster/map.rb +23 -0
  227. data/lib/active_record/validations.rb +93 -0
  228. data/lib/active_record/validations/absence.rb +25 -0
  229. data/lib/active_record/validations/associated.rb +60 -0
  230. data/lib/active_record/validations/length.rb +26 -0
  231. data/lib/active_record/validations/presence.rb +68 -0
  232. data/lib/active_record/validations/uniqueness.rb +238 -0
  233. data/lib/active_record/version.rb +10 -0
  234. data/lib/rails/generators/active_record.rb +19 -0
  235. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  236. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  237. data/lib/rails/generators/active_record/migration.rb +35 -0
  238. data/lib/rails/generators/active_record/migration/migration_generator.rb +78 -0
  239. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  240. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
  241. data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
  242. data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
  243. data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
  244. metadata +333 -0
@@ -0,0 +1,1131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Associations
5
+ # Association proxies in Active Record are middlemen between the object that
6
+ # holds the association, known as the <tt>@owner</tt>, and the actual associated
7
+ # object, known as the <tt>@target</tt>. The kind of association any proxy is
8
+ # about is available in <tt>@reflection</tt>. That's an instance of the class
9
+ # ActiveRecord::Reflection::AssociationReflection.
10
+ #
11
+ # For example, given
12
+ #
13
+ # class Blog < ActiveRecord::Base
14
+ # has_many :posts
15
+ # end
16
+ #
17
+ # blog = Blog.first
18
+ #
19
+ # the association proxy in <tt>blog.posts</tt> has the object in +blog+ as
20
+ # <tt>@owner</tt>, the collection of its posts as <tt>@target</tt>, and
21
+ # the <tt>@reflection</tt> object represents a <tt>:has_many</tt> macro.
22
+ #
23
+ # This class delegates unknown methods to <tt>@target</tt> via
24
+ # <tt>method_missing</tt>.
25
+ #
26
+ # The <tt>@target</tt> object is not \loaded until needed. For example,
27
+ #
28
+ # blog.posts.count
29
+ #
30
+ # is computed directly through SQL and does not trigger by itself the
31
+ # instantiation of the actual post records.
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
40
+
41
+ def target
42
+ @association.target
43
+ end
44
+
45
+ def load_target
46
+ @association.load_target
47
+ end
48
+
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
57
+
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
+ # # ]
111
+
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)
141
+ end
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
321
+ alias_method :new, :build
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
+
926
+ def proxy_association
927
+ @association
928
+ end
929
+
930
+ # Returns a <tt>Relation</tt> object for the records in this association
931
+ def scope
932
+ @scope ||= @association.scope
933
+ end
934
+
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
961
+ end
962
+
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
+ # # ]
1002
+
1003
+ def records # :nodoc:
1004
+ load_target
1005
+ end
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
+ # # ]
1028
+ def <<(*records)
1029
+ proxy_association.concat(records) && self
1030
+ end
1031
+ alias_method :push, :<<
1032
+ alias_method :append, :<<
1033
+ alias_method :concat, :<<
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.
1045
+ def clear
1046
+ delete_all
1047
+ self
1048
+ end
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>]
1064
+ def reload
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
1094
+ self
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
1129
+ end
1130
+ end
1131
+ end