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,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # = Active Record No Touching
5
+ module NoTouching
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ # Lets you selectively disable calls to +touch+ for the
10
+ # duration of a block.
11
+ #
12
+ # ==== Examples
13
+ # ActiveRecord::Base.no_touching do
14
+ # Project.first.touch # does nothing
15
+ # Message.first.touch # does nothing
16
+ # end
17
+ #
18
+ # Project.no_touching do
19
+ # Project.first.touch # does nothing
20
+ # Message.first.touch # works, but does not touch the associated project
21
+ # end
22
+ #
23
+ def no_touching(&block)
24
+ NoTouching.apply_to(self, &block)
25
+ end
26
+ end
27
+
28
+ class << self
29
+ def apply_to(klass) #:nodoc:
30
+ klasses.push(klass)
31
+ yield
32
+ ensure
33
+ klasses.pop
34
+ end
35
+
36
+ def applied_to?(klass) #:nodoc:
37
+ klasses.any? { |k| k >= klass }
38
+ end
39
+
40
+ private
41
+ def klasses
42
+ Thread.current[:no_touching_classes] ||= []
43
+ end
44
+ end
45
+
46
+ def no_touching?
47
+ NoTouching.applied_to?(self.class)
48
+ end
49
+
50
+ def touch_later(*) # :nodoc:
51
+ super unless no_touching?
52
+ end
53
+
54
+ def touch(*) # :nodoc:
55
+ super unless no_touching?
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module NullRelation # :nodoc:
5
+ def pluck(*column_names)
6
+ []
7
+ end
8
+
9
+ def delete_all
10
+ 0
11
+ end
12
+
13
+ def update_all(_updates)
14
+ 0
15
+ end
16
+
17
+ def delete(_id_or_array)
18
+ 0
19
+ end
20
+
21
+ def empty?
22
+ true
23
+ end
24
+
25
+ def none?
26
+ true
27
+ end
28
+
29
+ def any?
30
+ false
31
+ end
32
+
33
+ def one?
34
+ false
35
+ end
36
+
37
+ def many?
38
+ false
39
+ end
40
+
41
+ def to_sql
42
+ ""
43
+ end
44
+
45
+ def calculate(operation, _column_name)
46
+ case operation
47
+ when :count, :sum
48
+ group_values.any? ? Hash.new : 0
49
+ when :average, :minimum, :maximum
50
+ group_values.any? ? Hash.new : nil
51
+ end
52
+ end
53
+
54
+ def exists?(_conditions = :none)
55
+ false
56
+ end
57
+
58
+ def or(other)
59
+ other.spawn
60
+ end
61
+
62
+ private
63
+
64
+ def exec_queries
65
+ @records = [].freeze
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,763 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # = Active Record \Persistence
5
+ module Persistence
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ # Creates an object (or multiple objects) and saves it to the database, if validations pass.
10
+ # The resulting object is returned whether the object was saved successfully to the database or not.
11
+ #
12
+ # The +attributes+ parameter can be either a Hash or an Array of Hashes. These Hashes describe the
13
+ # attributes on the objects that are to be created.
14
+ #
15
+ # ==== Examples
16
+ # # Create a single new object
17
+ # User.create(first_name: 'Jamie')
18
+ #
19
+ # # Create an Array of new objects
20
+ # User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])
21
+ #
22
+ # # Create a single object and pass it into a block to set other attributes.
23
+ # User.create(first_name: 'Jamie') do |u|
24
+ # u.is_admin = false
25
+ # end
26
+ #
27
+ # # Creating an Array of new objects using a block, where the block is executed for each object:
28
+ # User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
29
+ # u.is_admin = false
30
+ # end
31
+ def create(attributes = nil, &block)
32
+ if attributes.is_a?(Array)
33
+ attributes.collect { |attr| create(attr, &block) }
34
+ else
35
+ object = new(attributes, &block)
36
+ object.save
37
+ object
38
+ end
39
+ end
40
+
41
+ # Creates an object (or multiple objects) and saves it to the database,
42
+ # if validations pass. Raises a RecordInvalid error if validations fail,
43
+ # unlike Base#create.
44
+ #
45
+ # The +attributes+ parameter can be either a Hash or an Array of Hashes.
46
+ # These describe which attributes to be created on the object, or
47
+ # multiple objects when given an Array of Hashes.
48
+ def create!(attributes = nil, &block)
49
+ if attributes.is_a?(Array)
50
+ attributes.collect { |attr| create!(attr, &block) }
51
+ else
52
+ object = new(attributes, &block)
53
+ object.save!
54
+ object
55
+ end
56
+ end
57
+
58
+ # Given an attributes hash, +instantiate+ returns a new instance of
59
+ # the appropriate class. Accepts only keys as strings.
60
+ #
61
+ # For example, +Post.all+ may return Comments, Messages, and Emails
62
+ # by storing the record's subclass in a +type+ attribute. By calling
63
+ # +instantiate+ instead of +new+, finder methods ensure they get new
64
+ # instances of the appropriate class for each record.
65
+ #
66
+ # See <tt>ActiveRecord::Inheritance#discriminate_class_for_record</tt> to see
67
+ # how this "single-table" inheritance mapping is implemented.
68
+ def instantiate(attributes, column_types = {}, &block)
69
+ klass = discriminate_class_for_record(attributes)
70
+ attributes = klass.attributes_builder.build_from_database(attributes, column_types)
71
+ klass.allocate.init_with("attributes" => attributes, "new_record" => false, &block)
72
+ end
73
+
74
+ # Updates an object (or multiple objects) and saves it to the database, if validations pass.
75
+ # The resulting object is returned whether the object was saved successfully to the database or not.
76
+ #
77
+ # ==== Parameters
78
+ #
79
+ # * +id+ - This should be the id or an array of ids to be updated.
80
+ # * +attributes+ - This should be a hash of attributes or an array of hashes.
81
+ #
82
+ # ==== Examples
83
+ #
84
+ # # Updates one record
85
+ # Person.update(15, user_name: "Samuel", group: "expert")
86
+ #
87
+ # # Updates multiple records
88
+ # people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
89
+ # Person.update(people.keys, people.values)
90
+ #
91
+ # # Updates multiple records from the result of a relation
92
+ # people = Person.where(group: "expert")
93
+ # people.update(group: "masters")
94
+ #
95
+ # Note: Updating a large number of records will run an UPDATE
96
+ # query for each record, which may cause a performance issue.
97
+ # When running callbacks is not needed for each record update,
98
+ # it is preferred to use {update_all}[rdoc-ref:Relation#update_all]
99
+ # for updating all records in a single query.
100
+ def update(id = :all, attributes)
101
+ if id.is_a?(Array)
102
+ id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
103
+ object.update(attributes[idx])
104
+ }
105
+ elsif id == :all
106
+ all.each { |record| record.update(attributes) }
107
+ else
108
+ if ActiveRecord::Base === id
109
+ raise ArgumentError,
110
+ "You are passing an instance of ActiveRecord::Base to `update`. " \
111
+ "Please pass the id of the object by calling `.id`."
112
+ end
113
+ object = find(id)
114
+ object.update(attributes)
115
+ object
116
+ end
117
+ end
118
+
119
+ # Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
120
+ # therefore all callbacks and filters are fired off before the object is deleted. This method is
121
+ # less efficient than #delete but allows cleanup methods and other actions to be run.
122
+ #
123
+ # This essentially finds the object (or multiple objects) with the given id, creates a new object
124
+ # from the attributes, and then calls destroy on it.
125
+ #
126
+ # ==== Parameters
127
+ #
128
+ # * +id+ - This should be the id or an array of ids to be destroyed.
129
+ #
130
+ # ==== Examples
131
+ #
132
+ # # Destroy a single object
133
+ # Todo.destroy(1)
134
+ #
135
+ # # Destroy multiple objects
136
+ # todos = [1,2,3]
137
+ # Todo.destroy(todos)
138
+ def destroy(id)
139
+ if id.is_a?(Array)
140
+ find(id).each(&:destroy)
141
+ else
142
+ find(id).destroy
143
+ end
144
+ end
145
+
146
+ # Deletes the row with a primary key matching the +id+ argument, using a
147
+ # SQL +DELETE+ statement, and returns the number of rows deleted. Active
148
+ # Record objects are not instantiated, so the object's callbacks are not
149
+ # executed, including any <tt>:dependent</tt> association options.
150
+ #
151
+ # You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
152
+ #
153
+ # Note: Although it is often much faster than the alternative, #destroy,
154
+ # skipping callbacks might bypass business logic in your application
155
+ # that ensures referential integrity or performs other essential jobs.
156
+ #
157
+ # ==== Examples
158
+ #
159
+ # # Delete a single row
160
+ # Todo.delete(1)
161
+ #
162
+ # # Delete multiple rows
163
+ # Todo.delete([2,3,4])
164
+ def delete(id_or_array)
165
+ where(primary_key => id_or_array).delete_all
166
+ end
167
+
168
+ def _insert_record(values) # :nodoc:
169
+ primary_key_value = nil
170
+
171
+ if primary_key && Hash === values
172
+ primary_key_value = values[primary_key]
173
+
174
+ if !primary_key_value && prefetch_primary_key?
175
+ primary_key_value = next_sequence_value
176
+ values[primary_key] = primary_key_value
177
+ end
178
+ end
179
+
180
+ if values.empty?
181
+ im = arel_table.compile_insert(connection.empty_insert_statement_value)
182
+ im.into arel_table
183
+ else
184
+ im = arel_table.compile_insert(_substitute_values(values))
185
+ end
186
+
187
+ connection.insert(im, "#{self} Create", primary_key || false, primary_key_value)
188
+ end
189
+
190
+ def _update_record(values, constraints) # :nodoc:
191
+ constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
192
+
193
+ um = arel_table.where(
194
+ constraints.reduce(&:and)
195
+ ).compile_update(_substitute_values(values), primary_key)
196
+
197
+ connection.update(um, "#{self} Update")
198
+ end
199
+
200
+ def _delete_record(constraints) # :nodoc:
201
+ constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
202
+
203
+ dm = Arel::DeleteManager.new
204
+ dm.from(arel_table)
205
+ dm.wheres = constraints
206
+
207
+ connection.delete(dm, "#{self} Destroy")
208
+ end
209
+
210
+ private
211
+ # Called by +instantiate+ to decide which class to use for a new
212
+ # record instance.
213
+ #
214
+ # See +ActiveRecord::Inheritance#discriminate_class_for_record+ for
215
+ # the single-table inheritance discriminator.
216
+ def discriminate_class_for_record(record)
217
+ self
218
+ end
219
+
220
+ def _substitute_values(values)
221
+ values.map do |name, value|
222
+ attr = arel_attribute(name)
223
+ bind = predicate_builder.build_bind_attribute(name, value)
224
+ [attr, bind]
225
+ end
226
+ end
227
+ end
228
+
229
+ # Returns true if this object hasn't been saved yet -- that is, a record
230
+ # for the object doesn't exist in the database yet; otherwise, returns false.
231
+ def new_record?
232
+ sync_with_transaction_state
233
+ @new_record
234
+ end
235
+
236
+ # Returns true if this object has been destroyed, otherwise returns false.
237
+ def destroyed?
238
+ sync_with_transaction_state
239
+ @destroyed
240
+ end
241
+
242
+ # Returns true if the record is persisted, i.e. it's not a new record and it was
243
+ # not destroyed, otherwise returns false.
244
+ def persisted?
245
+ sync_with_transaction_state
246
+ !(@new_record || @destroyed)
247
+ end
248
+
249
+ ##
250
+ # :call-seq:
251
+ # save(*args)
252
+ #
253
+ # Saves the model.
254
+ #
255
+ # If the model is new, a record gets created in the database, otherwise
256
+ # the existing record gets updated.
257
+ #
258
+ # By default, save always runs validations. If any of them fail the action
259
+ # is cancelled and #save returns +false+, and the record won't be saved. However, if you supply
260
+ # <tt>validate: false</tt>, validations are bypassed altogether. See
261
+ # ActiveRecord::Validations for more information.
262
+ #
263
+ # By default, #save also sets the +updated_at+/+updated_on+ attributes to
264
+ # the current time. However, if you supply <tt>touch: false</tt>, these
265
+ # timestamps will not be updated.
266
+ #
267
+ # There's a series of callbacks associated with #save. If any of the
268
+ # <tt>before_*</tt> callbacks throws +:abort+ the action is cancelled and
269
+ # #save returns +false+. See ActiveRecord::Callbacks for further
270
+ # details.
271
+ #
272
+ # Attributes marked as readonly are silently ignored if the record is
273
+ # being updated.
274
+ def save(*args, &block)
275
+ create_or_update(*args, &block)
276
+ rescue ActiveRecord::RecordInvalid
277
+ false
278
+ end
279
+
280
+ ##
281
+ # :call-seq:
282
+ # save!(*args)
283
+ #
284
+ # Saves the model.
285
+ #
286
+ # If the model is new, a record gets created in the database, otherwise
287
+ # the existing record gets updated.
288
+ #
289
+ # By default, #save! always runs validations. If any of them fail
290
+ # ActiveRecord::RecordInvalid gets raised, and the record won't be saved. However, if you supply
291
+ # <tt>validate: false</tt>, validations are bypassed altogether. See
292
+ # ActiveRecord::Validations for more information.
293
+ #
294
+ # By default, #save! also sets the +updated_at+/+updated_on+ attributes to
295
+ # the current time. However, if you supply <tt>touch: false</tt>, these
296
+ # timestamps will not be updated.
297
+ #
298
+ # There's a series of callbacks associated with #save!. If any of
299
+ # the <tt>before_*</tt> callbacks throws +:abort+ the action is cancelled
300
+ # and #save! raises ActiveRecord::RecordNotSaved. See
301
+ # ActiveRecord::Callbacks for further details.
302
+ #
303
+ # Attributes marked as readonly are silently ignored if the record is
304
+ # being updated.
305
+ #
306
+ # Unless an error is raised, returns true.
307
+ def save!(*args, &block)
308
+ create_or_update(*args, &block) || raise(RecordNotSaved.new("Failed to save the record", self))
309
+ end
310
+
311
+ # Deletes the record in the database and freezes this instance to
312
+ # reflect that no changes should be made (since they can't be
313
+ # persisted). Returns the frozen instance.
314
+ #
315
+ # The row is simply removed with an SQL +DELETE+ statement on the
316
+ # record's primary key, and no callbacks are executed.
317
+ #
318
+ # Note that this will also delete records marked as {#readonly?}[rdoc-ref:Core#readonly?].
319
+ #
320
+ # To enforce the object's +before_destroy+ and +after_destroy+
321
+ # callbacks or any <tt>:dependent</tt> association
322
+ # options, use <tt>#destroy</tt>.
323
+ def delete
324
+ _delete_row if persisted?
325
+ @destroyed = true
326
+ freeze
327
+ end
328
+
329
+ # Deletes the record in the database and freezes this instance to reflect
330
+ # that no changes should be made (since they can't be persisted).
331
+ #
332
+ # There's a series of callbacks associated with #destroy. If the
333
+ # <tt>before_destroy</tt> callback throws +:abort+ the action is cancelled
334
+ # and #destroy returns +false+.
335
+ # See ActiveRecord::Callbacks for further details.
336
+ def destroy
337
+ _raise_readonly_record_error if readonly?
338
+ destroy_associations
339
+ self.class.connection.add_transaction_record(self)
340
+ @_trigger_destroy_callback = if persisted?
341
+ destroy_row > 0
342
+ else
343
+ true
344
+ end
345
+ @destroyed = true
346
+ freeze
347
+ end
348
+
349
+ # Deletes the record in the database and freezes this instance to reflect
350
+ # that no changes should be made (since they can't be persisted).
351
+ #
352
+ # There's a series of callbacks associated with #destroy!. If the
353
+ # <tt>before_destroy</tt> callback throws +:abort+ the action is cancelled
354
+ # and #destroy! raises ActiveRecord::RecordNotDestroyed.
355
+ # See ActiveRecord::Callbacks for further details.
356
+ def destroy!
357
+ destroy || _raise_record_not_destroyed
358
+ end
359
+
360
+ # Returns an instance of the specified +klass+ with the attributes of the
361
+ # current record. This is mostly useful in relation to single-table
362
+ # inheritance structures where you want a subclass to appear as the
363
+ # superclass. This can be used along with record identification in
364
+ # Action Pack to allow, say, <tt>Client < Company</tt> to do something
365
+ # like render <tt>partial: @client.becomes(Company)</tt> to render that
366
+ # instance using the companies/company partial instead of clients/client.
367
+ #
368
+ # Note: The new instance will share a link to the same attributes as the original class.
369
+ # Therefore the sti column value will still be the same.
370
+ # Any change to the attributes on either instance will affect both instances.
371
+ # If you want to change the sti column as well, use #becomes! instead.
372
+ def becomes(klass)
373
+ became = klass.allocate
374
+ became.send(:initialize)
375
+ became.instance_variable_set("@attributes", @attributes)
376
+ became.instance_variable_set("@mutations_from_database", @mutations_from_database ||= nil)
377
+ became.instance_variable_set("@changed_attributes", attributes_changed_by_setter)
378
+ became.instance_variable_set("@new_record", new_record?)
379
+ became.instance_variable_set("@destroyed", destroyed?)
380
+ became.errors.copy!(errors)
381
+ became
382
+ end
383
+
384
+ # Wrapper around #becomes that also changes the instance's sti column value.
385
+ # This is especially useful if you want to persist the changed class in your
386
+ # database.
387
+ #
388
+ # Note: The old instance's sti column value will be changed too, as both objects
389
+ # share the same set of attributes.
390
+ def becomes!(klass)
391
+ became = becomes(klass)
392
+ sti_type = nil
393
+ if !klass.descends_from_active_record?
394
+ sti_type = klass.sti_name
395
+ end
396
+ became.public_send("#{klass.inheritance_column}=", sti_type)
397
+ became
398
+ end
399
+
400
+ # Updates a single attribute and saves the record.
401
+ # This is especially useful for boolean flags on existing records. Also note that
402
+ #
403
+ # * Validation is skipped.
404
+ # * \Callbacks are invoked.
405
+ # * updated_at/updated_on column is updated if that column is available.
406
+ # * Updates all the attributes that are dirty in this object.
407
+ #
408
+ # This method raises an ActiveRecord::ActiveRecordError if the
409
+ # attribute is marked as readonly.
410
+ #
411
+ # Also see #update_column.
412
+ def update_attribute(name, value)
413
+ name = name.to_s
414
+ verify_readonly_attribute(name)
415
+ public_send("#{name}=", value)
416
+
417
+ save(validate: false)
418
+ end
419
+
420
+ # Updates the attributes of the model from the passed-in hash and saves the
421
+ # record, all wrapped in a transaction. If the object is invalid, the saving
422
+ # will fail and false will be returned.
423
+ def update(attributes)
424
+ # The following transaction covers any possible database side-effects of the
425
+ # attributes assignment. For example, setting the IDs of a child collection.
426
+ with_transaction_returning_status do
427
+ assign_attributes(attributes)
428
+ save
429
+ end
430
+ end
431
+
432
+ alias update_attributes update
433
+
434
+ # Updates its receiver just like #update but calls #save! instead
435
+ # of +save+, so an exception is raised if the record is invalid and saving will fail.
436
+ def update!(attributes)
437
+ # The following transaction covers any possible database side-effects of the
438
+ # attributes assignment. For example, setting the IDs of a child collection.
439
+ with_transaction_returning_status do
440
+ assign_attributes(attributes)
441
+ save!
442
+ end
443
+ end
444
+
445
+ alias update_attributes! update!
446
+
447
+ # Equivalent to <code>update_columns(name => value)</code>.
448
+ def update_column(name, value)
449
+ update_columns(name => value)
450
+ end
451
+
452
+ # Updates the attributes directly in the database issuing an UPDATE SQL
453
+ # statement and sets them in the receiver:
454
+ #
455
+ # user.update_columns(last_request_at: Time.current)
456
+ #
457
+ # This is the fastest way to update attributes because it goes straight to
458
+ # the database, but take into account that in consequence the regular update
459
+ # procedures are totally bypassed. In particular:
460
+ #
461
+ # * \Validations are skipped.
462
+ # * \Callbacks are skipped.
463
+ # * +updated_at+/+updated_on+ are not updated.
464
+ # * However, attributes are serialized with the same rules as ActiveRecord::Relation#update_all
465
+ #
466
+ # This method raises an ActiveRecord::ActiveRecordError when called on new
467
+ # objects, or when at least one of the attributes is marked as readonly.
468
+ def update_columns(attributes)
469
+ raise ActiveRecordError, "cannot update a new record" if new_record?
470
+ raise ActiveRecordError, "cannot update a destroyed record" if destroyed?
471
+
472
+ attributes.each_key do |key|
473
+ verify_readonly_attribute(key.to_s)
474
+ end
475
+
476
+ id_in_database = self.id_in_database
477
+ attributes.each do |k, v|
478
+ write_attribute_without_type_cast(k, v)
479
+ end
480
+
481
+ affected_rows = self.class._update_record(
482
+ attributes,
483
+ self.class.primary_key => id_in_database
484
+ )
485
+
486
+ affected_rows == 1
487
+ end
488
+
489
+ # Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
490
+ # The increment is performed directly on the underlying attribute, no setter is invoked.
491
+ # Only makes sense for number-based attributes. Returns +self+.
492
+ def increment(attribute, by = 1)
493
+ self[attribute] ||= 0
494
+ self[attribute] += by
495
+ self
496
+ end
497
+
498
+ # Wrapper around #increment that writes the update to the database.
499
+ # Only +attribute+ is updated; the record itself is not saved.
500
+ # This means that any other modified attributes will still be dirty.
501
+ # Validations and callbacks are skipped. Supports the +touch+ option from
502
+ # +update_counters+, see that for more.
503
+ # Returns +self+.
504
+ def increment!(attribute, by = 1, touch: nil)
505
+ increment(attribute, by)
506
+ change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
507
+ self.class.update_counters(id, attribute => change, touch: touch)
508
+ clear_attribute_change(attribute) # eww
509
+ self
510
+ end
511
+
512
+ # Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
513
+ # The decrement is performed directly on the underlying attribute, no setter is invoked.
514
+ # Only makes sense for number-based attributes. Returns +self+.
515
+ def decrement(attribute, by = 1)
516
+ increment(attribute, -by)
517
+ end
518
+
519
+ # Wrapper around #decrement that writes the update to the database.
520
+ # Only +attribute+ is updated; the record itself is not saved.
521
+ # This means that any other modified attributes will still be dirty.
522
+ # Validations and callbacks are skipped. Supports the +touch+ option from
523
+ # +update_counters+, see that for more.
524
+ # Returns +self+.
525
+ def decrement!(attribute, by = 1, touch: nil)
526
+ increment!(attribute, -by, touch: touch)
527
+ end
528
+
529
+ # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
530
+ # if the predicate returns +true+ the attribute will become +false+. This
531
+ # method toggles directly the underlying value without calling any setter.
532
+ # Returns +self+.
533
+ #
534
+ # Example:
535
+ #
536
+ # user = User.first
537
+ # user.banned? # => false
538
+ # user.toggle(:banned)
539
+ # user.banned? # => true
540
+ #
541
+ def toggle(attribute)
542
+ self[attribute] = !public_send("#{attribute}?")
543
+ self
544
+ end
545
+
546
+ # Wrapper around #toggle that saves the record. This method differs from
547
+ # its non-bang version in the sense that it passes through the attribute setter.
548
+ # Saving is not subjected to validation checks. Returns +true+ if the
549
+ # record could be saved.
550
+ def toggle!(attribute)
551
+ toggle(attribute).update_attribute(attribute, self[attribute])
552
+ end
553
+
554
+ # Reloads the record from the database.
555
+ #
556
+ # This method finds the record by its primary key (which could be assigned
557
+ # manually) and modifies the receiver in-place:
558
+ #
559
+ # account = Account.new
560
+ # # => #<Account id: nil, email: nil>
561
+ # account.id = 1
562
+ # account.reload
563
+ # # Account Load (1.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT 1 [["id", 1]]
564
+ # # => #<Account id: 1, email: 'account@example.com'>
565
+ #
566
+ # Attributes are reloaded from the database, and caches busted, in
567
+ # particular the associations cache and the QueryCache.
568
+ #
569
+ # If the record no longer exists in the database ActiveRecord::RecordNotFound
570
+ # is raised. Otherwise, in addition to the in-place modification the method
571
+ # returns +self+ for convenience.
572
+ #
573
+ # The optional <tt>:lock</tt> flag option allows you to lock the reloaded record:
574
+ #
575
+ # reload(lock: true) # reload with pessimistic locking
576
+ #
577
+ # Reloading is commonly used in test suites to test something is actually
578
+ # written to the database, or when some action modifies the corresponding
579
+ # row in the database but not the object in memory:
580
+ #
581
+ # assert account.deposit!(25)
582
+ # assert_equal 25, account.credit # check it is updated in memory
583
+ # assert_equal 25, account.reload.credit # check it is also persisted
584
+ #
585
+ # Another common use case is optimistic locking handling:
586
+ #
587
+ # def with_optimistic_retry
588
+ # begin
589
+ # yield
590
+ # rescue ActiveRecord::StaleObjectError
591
+ # begin
592
+ # # Reload lock_version in particular.
593
+ # reload
594
+ # rescue ActiveRecord::RecordNotFound
595
+ # # If the record is gone there is nothing to do.
596
+ # else
597
+ # retry
598
+ # end
599
+ # end
600
+ # end
601
+ #
602
+ def reload(options = nil)
603
+ self.class.connection.clear_query_cache
604
+
605
+ fresh_object =
606
+ if options && options[:lock]
607
+ self.class.unscoped { self.class.lock(options[:lock]).find(id) }
608
+ else
609
+ self.class.unscoped { self.class.find(id) }
610
+ end
611
+
612
+ @attributes = fresh_object.instance_variable_get("@attributes")
613
+ @new_record = false
614
+ self
615
+ end
616
+
617
+ # Saves the record with the updated_at/on attributes set to the current time
618
+ # or the time specified.
619
+ # Please note that no validation is performed and only the +after_touch+,
620
+ # +after_commit+ and +after_rollback+ callbacks are executed.
621
+ #
622
+ # This method can be passed attribute names and an optional time argument.
623
+ # If attribute names are passed, they are updated along with updated_at/on
624
+ # attributes. If no time argument is passed, the current time is used as default.
625
+ #
626
+ # product.touch # updates updated_at/on with current time
627
+ # product.touch(time: Time.new(2015, 2, 16, 0, 0, 0)) # updates updated_at/on with specified time
628
+ # product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
629
+ # product.touch(:started_at, :ended_at) # updates started_at, ended_at and updated_at/on attributes
630
+ #
631
+ # If used along with {belongs_to}[rdoc-ref:Associations::ClassMethods#belongs_to]
632
+ # then +touch+ will invoke +touch+ method on associated object.
633
+ #
634
+ # class Brake < ActiveRecord::Base
635
+ # belongs_to :car, touch: true
636
+ # end
637
+ #
638
+ # class Car < ActiveRecord::Base
639
+ # belongs_to :corporation, touch: true
640
+ # end
641
+ #
642
+ # # triggers @brake.car.touch and @brake.car.corporation.touch
643
+ # @brake.touch
644
+ #
645
+ # Note that +touch+ must be used on a persisted object, or else an
646
+ # ActiveRecordError will be thrown. For example:
647
+ #
648
+ # ball = Ball.new
649
+ # ball.touch(:updated_at) # => raises ActiveRecordError
650
+ #
651
+ def touch(*names, time: nil)
652
+ unless persisted?
653
+ raise ActiveRecordError, <<-MSG.squish
654
+ cannot touch on a new or destroyed record object. Consider using
655
+ persisted?, new_record?, or destroyed? before touching
656
+ MSG
657
+ end
658
+
659
+ attribute_names = timestamp_attributes_for_update_in_model
660
+ attribute_names |= names.map(&:to_s)
661
+
662
+ unless attribute_names.empty?
663
+ affected_rows = _touch_row(attribute_names, time)
664
+ @_trigger_update_callback = affected_rows == 1
665
+ else
666
+ true
667
+ end
668
+ end
669
+
670
+ private
671
+
672
+ # A hook to be overridden by association modules.
673
+ def destroy_associations
674
+ end
675
+
676
+ def destroy_row
677
+ _delete_row
678
+ end
679
+
680
+ def _delete_row
681
+ self.class._delete_record(self.class.primary_key => id_in_database)
682
+ end
683
+
684
+ def _touch_row(attribute_names, time)
685
+ time ||= current_time_from_proper_timezone
686
+
687
+ attribute_names.each do |attr_name|
688
+ write_attribute(attr_name, time)
689
+ clear_attribute_change(attr_name)
690
+ end
691
+
692
+ _update_row(attribute_names, "touch")
693
+ end
694
+
695
+ def _update_row(attribute_names, attempted_action = "update")
696
+ self.class._update_record(
697
+ attributes_with_values(attribute_names),
698
+ self.class.primary_key => id_in_database
699
+ )
700
+ end
701
+
702
+ def create_or_update(*args, &block)
703
+ _raise_readonly_record_error if readonly?
704
+ return false if destroyed?
705
+ result = new_record? ? _create_record(&block) : _update_record(*args, &block)
706
+ result != false
707
+ end
708
+
709
+ # Updates the associated record with values matching those of the instance attributes.
710
+ # Returns the number of affected rows.
711
+ def _update_record(attribute_names = self.attribute_names)
712
+ attribute_names &= self.class.column_names
713
+ attribute_names = attributes_for_update(attribute_names)
714
+
715
+ if attribute_names.empty?
716
+ affected_rows = 0
717
+ @_trigger_update_callback = true
718
+ else
719
+ affected_rows = _update_row(attribute_names)
720
+ @_trigger_update_callback = affected_rows == 1
721
+ end
722
+
723
+ yield(self) if block_given?
724
+
725
+ affected_rows
726
+ end
727
+
728
+ # Creates a record with values matching those of the instance attributes
729
+ # and returns its id.
730
+ def _create_record(attribute_names = self.attribute_names)
731
+ attribute_names &= self.class.column_names
732
+ attributes_values = attributes_with_values_for_create(attribute_names)
733
+
734
+ new_id = self.class._insert_record(attributes_values)
735
+ self.id ||= new_id if self.class.primary_key
736
+
737
+ @new_record = false
738
+
739
+ yield(self) if block_given?
740
+
741
+ id
742
+ end
743
+
744
+ def verify_readonly_attribute(name)
745
+ raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
746
+ end
747
+
748
+ def _raise_record_not_destroyed
749
+ @_association_destroy_exception ||= nil
750
+ raise @_association_destroy_exception || RecordNotDestroyed.new("Failed to destroy the record", self)
751
+ ensure
752
+ @_association_destroy_exception = nil
753
+ end
754
+
755
+ def belongs_to_touch_method
756
+ :touch
757
+ end
758
+
759
+ def _raise_readonly_record_error
760
+ raise ReadOnlyRecord, "#{self.class} is marked as readonly"
761
+ end
762
+ end
763
+ end