activerecord 5.0.6 → 6.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +638 -2023
- data/MIT-LICENSE +3 -1
- data/README.rdoc +8 -6
- data/examples/performance.rb +31 -29
- data/examples/simple.rb +5 -3
- data/lib/active_record/aggregations.rb +249 -246
- data/lib/active_record/association_relation.rb +24 -13
- data/lib/active_record/associations/alias_tracker.rb +24 -33
- data/lib/active_record/associations/association.rb +119 -56
- data/lib/active_record/associations/association_scope.rb +94 -94
- data/lib/active_record/associations/belongs_to_association.rb +58 -42
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
- data/lib/active_record/associations/builder/association.rb +18 -25
- data/lib/active_record/associations/builder/belongs_to.rb +43 -54
- data/lib/active_record/associations/builder/collection_association.rb +7 -18
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +42 -61
- data/lib/active_record/associations/builder/has_many.rb +4 -0
- data/lib/active_record/associations/builder/has_one.rb +37 -1
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +80 -252
- data/lib/active_record/associations/collection_proxy.rb +158 -121
- data/lib/active_record/associations/foreign_association.rb +9 -0
- data/lib/active_record/associations/has_many_association.rb +23 -29
- data/lib/active_record/associations/has_many_through_association.rb +58 -44
- data/lib/active_record/associations/has_one_association.rb +59 -54
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +38 -90
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
- data/lib/active_record/associations/join_dependency.rb +134 -176
- data/lib/active_record/associations/preloader/association.rb +84 -125
- data/lib/active_record/associations/preloader/through_association.rb +82 -75
- data/lib/active_record/associations/preloader.rb +90 -102
- data/lib/active_record/associations/singular_association.rb +12 -45
- data/lib/active_record/associations/through_association.rb +26 -14
- data/lib/active_record/associations.rb +1603 -1592
- data/lib/active_record/attribute_assignment.rb +54 -60
- data/lib/active_record/attribute_decorators.rb +38 -15
- data/lib/active_record/attribute_methods/before_type_cast.rb +12 -7
- data/lib/active_record/attribute_methods/dirty.rb +179 -109
- data/lib/active_record/attribute_methods/primary_key.rb +86 -91
- data/lib/active_record/attribute_methods/query.rb +4 -3
- data/lib/active_record/attribute_methods/read.rb +21 -49
- data/lib/active_record/attribute_methods/serialization.rb +30 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -64
- data/lib/active_record/attribute_methods/write.rb +35 -33
- data/lib/active_record/attribute_methods.rb +66 -106
- data/lib/active_record/attributes.rb +38 -24
- data/lib/active_record/autosave_association.rb +53 -32
- data/lib/active_record/base.rb +27 -24
- data/lib/active_record/callbacks.rb +63 -33
- data/lib/active_record/coders/json.rb +2 -0
- data/lib/active_record/coders/yaml_column.rb +11 -11
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +553 -321
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +23 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +213 -94
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +59 -28
- data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -75
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +33 -27
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +207 -126
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +68 -80
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +369 -199
- data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
- data/lib/active_record/connection_adapters/abstract_adapter.rb +363 -202
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +405 -551
- data/lib/active_record/connection_adapters/column.rb +41 -13
- data/lib/active_record/connection_adapters/connection_specification.rb +172 -138
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +11 -4
- data/lib/active_record/connection_adapters/mysql/column.rb +8 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +143 -49
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +24 -22
- data/lib/active_record/connection_adapters/mysql/quoting.rb +50 -20
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +50 -45
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +58 -56
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +70 -36
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +12 -13
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +49 -30
- data/lib/active_record/connection_adapters/postgresql/column.rb +22 -7
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +60 -54
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +22 -10
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +6 -5
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +4 -2
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +19 -17
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -5
- data/lib/active_record/connection_adapters/postgresql/oid/{json.rb → oid.rb} +6 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +31 -9
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +34 -30
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -4
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +24 -21
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +95 -35
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +147 -105
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +35 -32
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +380 -300
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +26 -25
- data/lib/active_record/connection_adapters/postgresql/utils.rb +10 -6
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +382 -275
- data/lib/active_record/connection_adapters/schema_cache.rb +46 -12
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +13 -8
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +3 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +74 -19
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +3 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +254 -262
- data/lib/active_record/connection_adapters/statement_pool.rb +9 -7
- data/lib/active_record/connection_handling.rb +159 -40
- data/lib/active_record/core.rb +202 -162
- data/lib/active_record/counter_cache.rb +57 -28
- data/lib/active_record/database_configurations/database_config.rb +37 -0
- data/lib/active_record/database_configurations/hash_config.rb +50 -0
- data/lib/active_record/database_configurations/url_config.rb +79 -0
- data/lib/active_record/database_configurations.rb +233 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -86
- data/lib/active_record/enum.rb +60 -23
- data/lib/active_record/errors.rb +114 -18
- data/lib/active_record/explain.rb +4 -3
- data/lib/active_record/explain_registry.rb +3 -1
- data/lib/active_record/explain_subscriber.rb +9 -4
- data/lib/active_record/fixture_set/file.rb +13 -8
- data/lib/active_record/fixture_set/model_metadata.rb +33 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +153 -0
- data/lib/active_record/fixture_set/table_rows.rb +47 -0
- data/lib/active_record/fixtures.rb +195 -502
- data/lib/active_record/gem_version.rb +4 -2
- data/lib/active_record/inheritance.rb +151 -97
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +116 -25
- data/lib/active_record/internal_metadata.rb +15 -18
- data/lib/active_record/legacy_yaml_adapter.rb +4 -2
- data/lib/active_record/locking/optimistic.rb +78 -87
- data/lib/active_record/locking/pessimistic.rb +18 -6
- data/lib/active_record/log_subscriber.rb +48 -29
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +143 -97
- data/lib/active_record/migration/compatibility.rb +174 -56
- data/lib/active_record/migration/join_table.rb +8 -6
- data/lib/active_record/migration.rb +367 -300
- data/lib/active_record/model_schema.rb +145 -139
- data/lib/active_record/nested_attributes.rb +214 -201
- data/lib/active_record/no_touching.rb +10 -1
- data/lib/active_record/null_relation.rb +13 -34
- data/lib/active_record/persistence.rb +442 -72
- data/lib/active_record/query_cache.rb +15 -14
- data/lib/active_record/querying.rb +36 -23
- data/lib/active_record/railtie.rb +128 -36
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +34 -33
- data/lib/active_record/railties/databases.rake +309 -177
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +211 -249
- data/lib/active_record/relation/batches/batch_enumerator.rb +3 -1
- data/lib/active_record/relation/batches.rb +99 -52
- data/lib/active_record/relation/calculations.rb +211 -172
- data/lib/active_record/relation/delegation.rb +67 -65
- data/lib/active_record/relation/finder_methods.rb +208 -247
- data/lib/active_record/relation/from_clause.rb +2 -8
- data/lib/active_record/relation/merger.rb +78 -61
- data/lib/active_record/relation/predicate_builder/array_handler.rb +20 -14
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +4 -3
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +6 -4
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +7 -18
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
- data/lib/active_record/relation/predicate_builder.rb +86 -104
- data/lib/active_record/relation/query_attribute.rb +33 -2
- data/lib/active_record/relation/query_methods.rb +458 -329
- data/lib/active_record/relation/record_fetch_warning.rb +5 -3
- data/lib/active_record/relation/spawn_methods.rb +8 -7
- data/lib/active_record/relation/where_clause.rb +111 -95
- data/lib/active_record/relation/where_clause_factory.rb +6 -11
- data/lib/active_record/relation.rb +429 -318
- data/lib/active_record/result.rb +69 -39
- data/lib/active_record/runtime_registry.rb +5 -3
- data/lib/active_record/sanitization.rb +83 -99
- data/lib/active_record/schema.rb +7 -14
- data/lib/active_record/schema_dumper.rb +71 -69
- data/lib/active_record/schema_migration.rb +15 -5
- data/lib/active_record/scoping/default.rb +93 -95
- data/lib/active_record/scoping/named.rb +45 -25
- data/lib/active_record/scoping.rb +20 -19
- data/lib/active_record/secure_token.rb +4 -2
- data/lib/active_record/serialization.rb +2 -0
- data/lib/active_record/statement_cache.rb +63 -28
- data/lib/active_record/store.rb +121 -41
- data/lib/active_record/suppressor.rb +4 -1
- data/lib/active_record/table_metadata.rb +26 -20
- data/lib/active_record/tasks/database_tasks.rb +276 -85
- data/lib/active_record/tasks/mysql_database_tasks.rb +54 -90
- data/lib/active_record/tasks/postgresql_database_tasks.rb +78 -47
- data/lib/active_record/tasks/sqlite_database_tasks.rb +34 -16
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +70 -35
- data/lib/active_record/touch_later.rb +7 -4
- data/lib/active_record/transactions.rb +133 -149
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +44 -45
- data/lib/active_record/type/date.rb +2 -0
- data/lib/active_record/type/date_time.rb +2 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +5 -3
- data/lib/active_record/type/internal/timezone.rb +2 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +16 -8
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +2 -1
- data/lib/active_record/type/type_map.rb +13 -15
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +23 -17
- data/lib/active_record/type_caster/connection.rb +17 -12
- data/lib/active_record/type_caster/map.rb +5 -4
- data/lib/active_record/type_caster.rb +4 -2
- data/lib/active_record/validations/absence.rb +2 -0
- data/lib/active_record/validations/associated.rb +3 -1
- data/lib/active_record/validations/length.rb +2 -0
- data/lib/active_record/validations/presence.rb +4 -2
- data/lib/active_record/validations/uniqueness.rb +29 -42
- data/lib/active_record/validations.rb +7 -4
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +36 -22
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/attributes.rb +22 -0
- data/lib/arel/collectors/bind.rb +24 -0
- data/lib/arel/collectors/composite.rb +31 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +20 -0
- data/lib/arel/collectors/substitute_binds.rb +28 -0
- data/lib/arel/crud.rb +42 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/errors.rb +9 -0
- data/lib/arel/expressions.rb +29 -0
- data/lib/arel/factory_methods.rb +49 -0
- data/lib/arel/insert_manager.rb +49 -0
- data/lib/arel/math.rb +45 -0
- data/lib/arel/nodes/and.rb +32 -0
- data/lib/arel/nodes/ascending.rb +23 -0
- data/lib/arel/nodes/binary.rb +52 -0
- data/lib/arel/nodes/bind_param.rb +36 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +50 -0
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/count.rb +12 -0
- data/lib/arel/nodes/delete_statement.rb +45 -0
- data/lib/arel/nodes/descending.rb +23 -0
- data/lib/arel/nodes/equality.rb +18 -0
- data/lib/arel/nodes/extract.rb +24 -0
- data/lib/arel/nodes/false.rb +16 -0
- data/lib/arel/nodes/full_outer_join.rb +8 -0
- data/lib/arel/nodes/function.rb +44 -0
- data/lib/arel/nodes/grouping.rb +8 -0
- data/lib/arel/nodes/in.rb +8 -0
- data/lib/arel/nodes/infix_operation.rb +80 -0
- data/lib/arel/nodes/inner_join.rb +8 -0
- data/lib/arel/nodes/insert_statement.rb +37 -0
- data/lib/arel/nodes/join_source.rb +20 -0
- data/lib/arel/nodes/matches.rb +18 -0
- data/lib/arel/nodes/named_function.rb +23 -0
- data/lib/arel/nodes/node.rb +50 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/outer_join.rb +8 -0
- data/lib/arel/nodes/over.rb +15 -0
- data/lib/arel/nodes/regexp.rb +16 -0
- data/lib/arel/nodes/right_outer_join.rb +8 -0
- data/lib/arel/nodes/select_core.rb +67 -0
- data/lib/arel/nodes/select_statement.rb +41 -0
- data/lib/arel/nodes/sql_literal.rb +16 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +27 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +45 -0
- data/lib/arel/nodes/unary_operation.rb +20 -0
- data/lib/arel/nodes/unqualified_column.rb +22 -0
- data/lib/arel/nodes/update_statement.rb +41 -0
- data/lib/arel/nodes/values_list.rb +9 -0
- data/lib/arel/nodes/window.rb +126 -0
- data/lib/arel/nodes/with.rb +11 -0
- data/lib/arel/nodes.rb +68 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +257 -0
- data/lib/arel/select_manager.rb +271 -0
- data/lib/arel/table.rb +110 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/depth_first.rb +204 -0
- data/lib/arel/visitors/dot.rb +297 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +157 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +159 -0
- data/lib/arel/visitors/oracle12.rb +66 -0
- data/lib/arel/visitors/postgresql.rb +110 -0
- data/lib/arel/visitors/sqlite.rb +39 -0
- data/lib/arel/visitors/to_sql.rb +889 -0
- data/lib/arel/visitors/visitor.rb +46 -0
- data/lib/arel/visitors/where_sql.rb +23 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +58 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -35
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +1 -1
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +4 -2
- data/lib/rails/generators/active_record/migration.rb +17 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -29
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +10 -1
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +133 -50
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -17
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -15
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -20
- data/lib/active_record/attribute/user_provided_default.rb +0 -28
- data/lib/active_record/attribute.rb +0 -213
- data/lib/active_record/attribute_mutation_tracker.rb +0 -70
- data/lib/active_record/attribute_set/builder.rb +0 -130
- data/lib/active_record/attribute_set.rb +0 -110
- data/lib/active_record/collection_cache_key.rb +0 -50
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +0 -50
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
- data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -57
- data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,3 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/insert_all"
|
4
|
+
|
1
5
|
module ActiveRecord
|
2
6
|
# = Active Record \Persistence
|
3
7
|
module Persistence
|
@@ -53,6 +57,192 @@ module ActiveRecord
|
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
60
|
+
# Inserts a single record into the database in a single SQL INSERT
|
61
|
+
# statement. It does not instantiate any models nor does it trigger
|
62
|
+
# Active Record callbacks or validations. Though passed values
|
63
|
+
# go through Active Record's type casting and serialization.
|
64
|
+
#
|
65
|
+
# See <tt>ActiveRecord::Persistence#insert_all</tt> for documentation.
|
66
|
+
def insert(attributes, returning: nil, unique_by: nil)
|
67
|
+
insert_all([ attributes ], returning: returning, unique_by: unique_by)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Inserts multiple records into the database in a single SQL INSERT
|
71
|
+
# statement. It does not instantiate any models nor does it trigger
|
72
|
+
# Active Record callbacks or validations. Though passed values
|
73
|
+
# go through Active Record's type casting and serialization.
|
74
|
+
#
|
75
|
+
# The +attributes+ parameter is an Array of Hashes. Every Hash determines
|
76
|
+
# the attributes for a single row and must have the same keys.
|
77
|
+
#
|
78
|
+
# Rows are considered to be unique by every unique index on the table. Any
|
79
|
+
# duplicate rows are skipped.
|
80
|
+
# Override with <tt>:unique_by</tt> (see below).
|
81
|
+
#
|
82
|
+
# Returns an <tt>ActiveRecord::Result</tt> with its contents based on
|
83
|
+
# <tt>:returning</tt> (see below).
|
84
|
+
#
|
85
|
+
# ==== Options
|
86
|
+
#
|
87
|
+
# [:returning]
|
88
|
+
# (PostgreSQL only) An array of attributes to return for all successfully
|
89
|
+
# inserted records, which by default is the primary key.
|
90
|
+
# Pass <tt>returning: %w[ id name ]</tt> for both id and name
|
91
|
+
# or <tt>returning: false</tt> to omit the underlying <tt>RETURNING</tt> SQL
|
92
|
+
# clause entirely.
|
93
|
+
#
|
94
|
+
# [:unique_by]
|
95
|
+
# (PostgreSQL and SQLite only) By default rows are considered to be unique
|
96
|
+
# by every unique index on the table. Any duplicate rows are skipped.
|
97
|
+
#
|
98
|
+
# To skip rows according to just one unique index pass <tt>:unique_by</tt>.
|
99
|
+
#
|
100
|
+
# Consider a Book model where no duplicate ISBNs make sense, but if any
|
101
|
+
# row has an existing id, or is not unique by another unique index,
|
102
|
+
# <tt>ActiveRecord::RecordNotUnique</tt> is raised.
|
103
|
+
#
|
104
|
+
# Unique indexes can be identified by columns or name:
|
105
|
+
#
|
106
|
+
# unique_by: :isbn
|
107
|
+
# unique_by: %i[ author_id name ]
|
108
|
+
# unique_by: :index_books_on_isbn
|
109
|
+
#
|
110
|
+
# Because it relies on the index information from the database
|
111
|
+
# <tt>:unique_by</tt> is recommended to be paired with
|
112
|
+
# Active Record's schema_cache.
|
113
|
+
#
|
114
|
+
# ==== Example
|
115
|
+
#
|
116
|
+
# # Insert records and skip inserting any duplicates.
|
117
|
+
# # Here "Eloquent Ruby" is skipped because its id is not unique.
|
118
|
+
#
|
119
|
+
# Book.insert_all([
|
120
|
+
# { id: 1, title: "Rework", author: "David" },
|
121
|
+
# { id: 1, title: "Eloquent Ruby", author: "Russ" }
|
122
|
+
# ])
|
123
|
+
def insert_all(attributes, returning: nil, unique_by: nil)
|
124
|
+
InsertAll.new(self, attributes, on_duplicate: :skip, returning: returning, unique_by: unique_by).execute
|
125
|
+
end
|
126
|
+
|
127
|
+
# Inserts a single record into the database in a single SQL INSERT
|
128
|
+
# statement. It does not instantiate any models nor does it trigger
|
129
|
+
# Active Record callbacks or validations. Though passed values
|
130
|
+
# go through Active Record's type casting and serialization.
|
131
|
+
#
|
132
|
+
# See <tt>ActiveRecord::Persistence#insert_all!</tt> for more.
|
133
|
+
def insert!(attributes, returning: nil)
|
134
|
+
insert_all!([ attributes ], returning: returning)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Inserts multiple records into the database in a single SQL INSERT
|
138
|
+
# statement. It does not instantiate any models nor does it trigger
|
139
|
+
# Active Record callbacks or validations. Though passed values
|
140
|
+
# go through Active Record's type casting and serialization.
|
141
|
+
#
|
142
|
+
# The +attributes+ parameter is an Array of Hashes. Every Hash determines
|
143
|
+
# the attributes for a single row and must have the same keys.
|
144
|
+
#
|
145
|
+
# Raises <tt>ActiveRecord::RecordNotUnique</tt> if any rows violate a
|
146
|
+
# unique index on the table. In that case, no rows are inserted.
|
147
|
+
#
|
148
|
+
# To skip duplicate rows, see <tt>ActiveRecord::Persistence#insert_all</tt>.
|
149
|
+
# To replace them, see <tt>ActiveRecord::Persistence#upsert_all</tt>.
|
150
|
+
#
|
151
|
+
# Returns an <tt>ActiveRecord::Result</tt> with its contents based on
|
152
|
+
# <tt>:returning</tt> (see below).
|
153
|
+
#
|
154
|
+
# ==== Options
|
155
|
+
#
|
156
|
+
# [:returning]
|
157
|
+
# (PostgreSQL only) An array of attributes to return for all successfully
|
158
|
+
# inserted records, which by default is the primary key.
|
159
|
+
# Pass <tt>returning: %w[ id name ]</tt> for both id and name
|
160
|
+
# or <tt>returning: false</tt> to omit the underlying <tt>RETURNING</tt> SQL
|
161
|
+
# clause entirely.
|
162
|
+
#
|
163
|
+
# ==== Examples
|
164
|
+
#
|
165
|
+
# # Insert multiple records
|
166
|
+
# Book.insert_all!([
|
167
|
+
# { title: "Rework", author: "David" },
|
168
|
+
# { title: "Eloquent Ruby", author: "Russ" }
|
169
|
+
# ])
|
170
|
+
#
|
171
|
+
# # Raises ActiveRecord::RecordNotUnique because "Eloquent Ruby"
|
172
|
+
# # does not have a unique id.
|
173
|
+
# Book.insert_all!([
|
174
|
+
# { id: 1, title: "Rework", author: "David" },
|
175
|
+
# { id: 1, title: "Eloquent Ruby", author: "Russ" }
|
176
|
+
# ])
|
177
|
+
def insert_all!(attributes, returning: nil)
|
178
|
+
InsertAll.new(self, attributes, on_duplicate: :raise, returning: returning).execute
|
179
|
+
end
|
180
|
+
|
181
|
+
# Updates or inserts (upserts) a single record into the database in a
|
182
|
+
# single SQL INSERT statement. It does not instantiate any models nor does
|
183
|
+
# it trigger Active Record callbacks or validations. Though passed values
|
184
|
+
# go through Active Record's type casting and serialization.
|
185
|
+
#
|
186
|
+
# See <tt>ActiveRecord::Persistence#upsert_all</tt> for documentation.
|
187
|
+
def upsert(attributes, returning: nil, unique_by: nil)
|
188
|
+
upsert_all([ attributes ], returning: returning, unique_by: unique_by)
|
189
|
+
end
|
190
|
+
|
191
|
+
# Updates or inserts (upserts) multiple records into the database in a
|
192
|
+
# single SQL INSERT statement. It does not instantiate any models nor does
|
193
|
+
# it trigger Active Record callbacks or validations. Though passed values
|
194
|
+
# go through Active Record's type casting and serialization.
|
195
|
+
#
|
196
|
+
# The +attributes+ parameter is an Array of Hashes. Every Hash determines
|
197
|
+
# the attributes for a single row and must have the same keys.
|
198
|
+
#
|
199
|
+
# Returns an <tt>ActiveRecord::Result</tt> with its contents based on
|
200
|
+
# <tt>:returning</tt> (see below).
|
201
|
+
#
|
202
|
+
# ==== Options
|
203
|
+
#
|
204
|
+
# [:returning]
|
205
|
+
# (PostgreSQL only) An array of attributes to return for all successfully
|
206
|
+
# inserted records, which by default is the primary key.
|
207
|
+
# Pass <tt>returning: %w[ id name ]</tt> for both id and name
|
208
|
+
# or <tt>returning: false</tt> to omit the underlying <tt>RETURNING</tt> SQL
|
209
|
+
# clause entirely.
|
210
|
+
#
|
211
|
+
# [:unique_by]
|
212
|
+
# (PostgreSQL and SQLite only) By default rows are considered to be unique
|
213
|
+
# by every unique index on the table. Any duplicate rows are skipped.
|
214
|
+
#
|
215
|
+
# To skip rows according to just one unique index pass <tt>:unique_by</tt>.
|
216
|
+
#
|
217
|
+
# Consider a Book model where no duplicate ISBNs make sense, but if any
|
218
|
+
# row has an existing id, or is not unique by another unique index,
|
219
|
+
# <tt>ActiveRecord::RecordNotUnique</tt> is raised.
|
220
|
+
#
|
221
|
+
# Unique indexes can be identified by columns or name:
|
222
|
+
#
|
223
|
+
# unique_by: :isbn
|
224
|
+
# unique_by: %i[ author_id name ]
|
225
|
+
# unique_by: :index_books_on_isbn
|
226
|
+
#
|
227
|
+
# Because it relies on the index information from the database
|
228
|
+
# <tt>:unique_by</tt> is recommended to be paired with
|
229
|
+
# Active Record's schema_cache.
|
230
|
+
#
|
231
|
+
# ==== Examples
|
232
|
+
#
|
233
|
+
# # Inserts multiple records, performing an upsert when records have duplicate ISBNs.
|
234
|
+
# # Here "Eloquent Ruby" overwrites "Rework" because its ISBN is duplicate.
|
235
|
+
#
|
236
|
+
# Book.upsert_all([
|
237
|
+
# { title: "Rework", author: "David", isbn: "1" },
|
238
|
+
# { title: "Eloquent Ruby", author: "Russ", isbn: "1" }
|
239
|
+
# ], unique_by: :isbn)
|
240
|
+
#
|
241
|
+
# Book.find_by(isbn: "1").title # => "Eloquent Ruby"
|
242
|
+
def upsert_all(attributes, returning: nil, unique_by: nil)
|
243
|
+
InsertAll.new(self, attributes, on_duplicate: :update, returning: returning, unique_by: unique_by).execute
|
244
|
+
end
|
245
|
+
|
56
246
|
# Given an attributes hash, +instantiate+ returns a new instance of
|
57
247
|
# the appropriate class. Accepts only keys as strings.
|
58
248
|
#
|
@@ -65,11 +255,154 @@ module ActiveRecord
|
|
65
255
|
# how this "single-table" inheritance mapping is implemented.
|
66
256
|
def instantiate(attributes, column_types = {}, &block)
|
67
257
|
klass = discriminate_class_for_record(attributes)
|
68
|
-
|
69
|
-
|
258
|
+
instantiate_instance_of(klass, attributes, column_types, &block)
|
259
|
+
end
|
260
|
+
|
261
|
+
# Updates an object (or multiple objects) and saves it to the database, if validations pass.
|
262
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
263
|
+
#
|
264
|
+
# ==== Parameters
|
265
|
+
#
|
266
|
+
# * +id+ - This should be the id or an array of ids to be updated.
|
267
|
+
# * +attributes+ - This should be a hash of attributes or an array of hashes.
|
268
|
+
#
|
269
|
+
# ==== Examples
|
270
|
+
#
|
271
|
+
# # Updates one record
|
272
|
+
# Person.update(15, user_name: "Samuel", group: "expert")
|
273
|
+
#
|
274
|
+
# # Updates multiple records
|
275
|
+
# people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
|
276
|
+
# Person.update(people.keys, people.values)
|
277
|
+
#
|
278
|
+
# # Updates multiple records from the result of a relation
|
279
|
+
# people = Person.where(group: "expert")
|
280
|
+
# people.update(group: "masters")
|
281
|
+
#
|
282
|
+
# Note: Updating a large number of records will run an UPDATE
|
283
|
+
# query for each record, which may cause a performance issue.
|
284
|
+
# When running callbacks is not needed for each record update,
|
285
|
+
# it is preferred to use {update_all}[rdoc-ref:Relation#update_all]
|
286
|
+
# for updating all records in a single query.
|
287
|
+
def update(id = :all, attributes)
|
288
|
+
if id.is_a?(Array)
|
289
|
+
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
|
290
|
+
object.update(attributes[idx])
|
291
|
+
}
|
292
|
+
elsif id == :all
|
293
|
+
all.each { |record| record.update(attributes) }
|
294
|
+
else
|
295
|
+
if ActiveRecord::Base === id
|
296
|
+
raise ArgumentError,
|
297
|
+
"You are passing an instance of ActiveRecord::Base to `update`. " \
|
298
|
+
"Please pass the id of the object by calling `.id`."
|
299
|
+
end
|
300
|
+
object = find(id)
|
301
|
+
object.update(attributes)
|
302
|
+
object
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
|
307
|
+
# therefore all callbacks and filters are fired off before the object is deleted. This method is
|
308
|
+
# less efficient than #delete but allows cleanup methods and other actions to be run.
|
309
|
+
#
|
310
|
+
# This essentially finds the object (or multiple objects) with the given id, creates a new object
|
311
|
+
# from the attributes, and then calls destroy on it.
|
312
|
+
#
|
313
|
+
# ==== Parameters
|
314
|
+
#
|
315
|
+
# * +id+ - This should be the id or an array of ids to be destroyed.
|
316
|
+
#
|
317
|
+
# ==== Examples
|
318
|
+
#
|
319
|
+
# # Destroy a single object
|
320
|
+
# Todo.destroy(1)
|
321
|
+
#
|
322
|
+
# # Destroy multiple objects
|
323
|
+
# todos = [1,2,3]
|
324
|
+
# Todo.destroy(todos)
|
325
|
+
def destroy(id)
|
326
|
+
if id.is_a?(Array)
|
327
|
+
find(id).each(&:destroy)
|
328
|
+
else
|
329
|
+
find(id).destroy
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
# Deletes the row with a primary key matching the +id+ argument, using an
|
334
|
+
# SQL +DELETE+ statement, and returns the number of rows deleted. Active
|
335
|
+
# Record objects are not instantiated, so the object's callbacks are not
|
336
|
+
# executed, including any <tt>:dependent</tt> association options.
|
337
|
+
#
|
338
|
+
# You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
|
339
|
+
#
|
340
|
+
# Note: Although it is often much faster than the alternative, #destroy,
|
341
|
+
# skipping callbacks might bypass business logic in your application
|
342
|
+
# that ensures referential integrity or performs other essential jobs.
|
343
|
+
#
|
344
|
+
# ==== Examples
|
345
|
+
#
|
346
|
+
# # Delete a single row
|
347
|
+
# Todo.delete(1)
|
348
|
+
#
|
349
|
+
# # Delete multiple rows
|
350
|
+
# Todo.delete([2,3,4])
|
351
|
+
def delete(id_or_array)
|
352
|
+
delete_by(primary_key => id_or_array)
|
353
|
+
end
|
354
|
+
|
355
|
+
def _insert_record(values) # :nodoc:
|
356
|
+
primary_key = self.primary_key
|
357
|
+
primary_key_value = nil
|
358
|
+
|
359
|
+
if primary_key && Hash === values
|
360
|
+
primary_key_value = values[primary_key]
|
361
|
+
|
362
|
+
if !primary_key_value && prefetch_primary_key?
|
363
|
+
primary_key_value = next_sequence_value
|
364
|
+
values[primary_key] = primary_key_value
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
if values.empty?
|
369
|
+
im = arel_table.compile_insert(connection.empty_insert_statement_value(primary_key))
|
370
|
+
im.into arel_table
|
371
|
+
else
|
372
|
+
im = arel_table.compile_insert(_substitute_values(values))
|
373
|
+
end
|
374
|
+
|
375
|
+
connection.insert(im, "#{self} Create", primary_key || false, primary_key_value)
|
376
|
+
end
|
377
|
+
|
378
|
+
def _update_record(values, constraints) # :nodoc:
|
379
|
+
constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
|
380
|
+
|
381
|
+
um = arel_table.where(
|
382
|
+
constraints.reduce(&:and)
|
383
|
+
).compile_update(_substitute_values(values), primary_key)
|
384
|
+
|
385
|
+
connection.update(um, "#{self} Update")
|
386
|
+
end
|
387
|
+
|
388
|
+
def _delete_record(constraints) # :nodoc:
|
389
|
+
constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
|
390
|
+
|
391
|
+
dm = Arel::DeleteManager.new
|
392
|
+
dm.from(arel_table)
|
393
|
+
dm.wheres = constraints
|
394
|
+
|
395
|
+
connection.delete(dm, "#{self} Destroy")
|
70
396
|
end
|
71
397
|
|
72
398
|
private
|
399
|
+
# Given a class, an attributes hash, +instantiate_instance_of+ returns a
|
400
|
+
# new instance of the class. Accepts only keys as strings.
|
401
|
+
def instantiate_instance_of(klass, attributes, column_types = {}, &block)
|
402
|
+
attributes = klass.attributes_builder.build_from_database(attributes, column_types)
|
403
|
+
klass.allocate.init_with_attributes(attributes, &block)
|
404
|
+
end
|
405
|
+
|
73
406
|
# Called by +instantiate+ to decide which class to use for a new
|
74
407
|
# record instance.
|
75
408
|
#
|
@@ -78,25 +411,33 @@ module ActiveRecord
|
|
78
411
|
def discriminate_class_for_record(record)
|
79
412
|
self
|
80
413
|
end
|
414
|
+
|
415
|
+
def _substitute_values(values)
|
416
|
+
values.map do |name, value|
|
417
|
+
attr = arel_attribute(name)
|
418
|
+
bind = predicate_builder.build_bind_attribute(name, value)
|
419
|
+
[attr, bind]
|
420
|
+
end
|
421
|
+
end
|
81
422
|
end
|
82
423
|
|
83
424
|
# Returns true if this object hasn't been saved yet -- that is, a record
|
84
425
|
# for the object doesn't exist in the database yet; otherwise, returns false.
|
85
426
|
def new_record?
|
86
|
-
sync_with_transaction_state
|
427
|
+
sync_with_transaction_state if @transaction_state&.finalized?
|
87
428
|
@new_record
|
88
429
|
end
|
89
430
|
|
90
431
|
# Returns true if this object has been destroyed, otherwise returns false.
|
91
432
|
def destroyed?
|
92
|
-
sync_with_transaction_state
|
433
|
+
sync_with_transaction_state if @transaction_state&.finalized?
|
93
434
|
@destroyed
|
94
435
|
end
|
95
436
|
|
96
437
|
# Returns true if the record is persisted, i.e. it's not a new record and it was
|
97
438
|
# not destroyed, otherwise returns false.
|
98
439
|
def persisted?
|
99
|
-
sync_with_transaction_state
|
440
|
+
sync_with_transaction_state if @transaction_state&.finalized?
|
100
441
|
!(@new_record || @destroyed)
|
101
442
|
end
|
102
443
|
|
@@ -111,7 +452,7 @@ module ActiveRecord
|
|
111
452
|
#
|
112
453
|
# By default, save always runs validations. If any of them fail the action
|
113
454
|
# is cancelled and #save returns +false+, and the record won't be saved. However, if you supply
|
114
|
-
# validate: false
|
455
|
+
# <tt>validate: false</tt>, validations are bypassed altogether. See
|
115
456
|
# ActiveRecord::Validations for more information.
|
116
457
|
#
|
117
458
|
# By default, #save also sets the +updated_at+/+updated_on+ attributes to
|
@@ -142,7 +483,7 @@ module ActiveRecord
|
|
142
483
|
#
|
143
484
|
# By default, #save! always runs validations. If any of them fail
|
144
485
|
# ActiveRecord::RecordInvalid gets raised, and the record won't be saved. However, if you supply
|
145
|
-
# validate: false
|
486
|
+
# <tt>validate: false</tt>, validations are bypassed altogether. See
|
146
487
|
# ActiveRecord::Validations for more information.
|
147
488
|
#
|
148
489
|
# By default, #save! also sets the +updated_at+/+updated_on+ attributes to
|
@@ -156,6 +497,8 @@ module ActiveRecord
|
|
156
497
|
#
|
157
498
|
# Attributes marked as readonly are silently ignored if the record is
|
158
499
|
# being updated.
|
500
|
+
#
|
501
|
+
# Unless an error is raised, returns true.
|
159
502
|
def save!(*args, &block)
|
160
503
|
create_or_update(*args, &block) || raise(RecordNotSaved.new("Failed to save the record", self))
|
161
504
|
end
|
@@ -173,7 +516,7 @@ module ActiveRecord
|
|
173
516
|
# callbacks or any <tt>:dependent</tt> association
|
174
517
|
# options, use <tt>#destroy</tt>.
|
175
518
|
def delete
|
176
|
-
|
519
|
+
_delete_row if persisted?
|
177
520
|
@destroyed = true
|
178
521
|
freeze
|
179
522
|
end
|
@@ -186,10 +529,13 @@ module ActiveRecord
|
|
186
529
|
# and #destroy returns +false+.
|
187
530
|
# See ActiveRecord::Callbacks for further details.
|
188
531
|
def destroy
|
189
|
-
|
532
|
+
_raise_readonly_record_error if readonly?
|
190
533
|
destroy_associations
|
191
|
-
|
192
|
-
|
534
|
+
@_trigger_destroy_callback = if persisted?
|
535
|
+
destroy_row > 0
|
536
|
+
else
|
537
|
+
true
|
538
|
+
end
|
193
539
|
@destroyed = true
|
194
540
|
freeze
|
195
541
|
end
|
@@ -218,10 +564,10 @@ module ActiveRecord
|
|
218
564
|
# Any change to the attributes on either instance will affect both instances.
|
219
565
|
# If you want to change the sti column as well, use #becomes! instead.
|
220
566
|
def becomes(klass)
|
221
|
-
became = klass.
|
567
|
+
became = klass.allocate
|
568
|
+
became.send(:initialize)
|
222
569
|
became.instance_variable_set("@attributes", @attributes)
|
223
|
-
became.instance_variable_set("@
|
224
|
-
became.instance_variable_set("@changed_attributes", attributes_changed_by_setter)
|
570
|
+
became.instance_variable_set("@mutations_from_database", @mutations_from_database ||= nil)
|
225
571
|
became.instance_variable_set("@new_record", new_record?)
|
226
572
|
became.instance_variable_set("@destroyed", destroyed?)
|
227
573
|
became.errors.copy!(errors)
|
@@ -261,7 +607,7 @@ module ActiveRecord
|
|
261
607
|
verify_readonly_attribute(name)
|
262
608
|
public_send("#{name}=", value)
|
263
609
|
|
264
|
-
|
610
|
+
save(validate: false)
|
265
611
|
end
|
266
612
|
|
267
613
|
# Updates the attributes of the model from the passed-in hash and saves the
|
@@ -277,6 +623,7 @@ module ActiveRecord
|
|
277
623
|
end
|
278
624
|
|
279
625
|
alias update_attributes update
|
626
|
+
deprecate update_attributes: "please, use update instead"
|
280
627
|
|
281
628
|
# Updates its receiver just like #update but calls #save! instead
|
282
629
|
# of +save+, so an exception is raised if the record is invalid and saving will fail.
|
@@ -290,6 +637,7 @@ module ActiveRecord
|
|
290
637
|
end
|
291
638
|
|
292
639
|
alias update_attributes! update!
|
640
|
+
deprecate update_attributes!: "please, use update! instead"
|
293
641
|
|
294
642
|
# Equivalent to <code>update_columns(name => value)</code>.
|
295
643
|
def update_column(name, value)
|
@@ -316,17 +664,26 @@ module ActiveRecord
|
|
316
664
|
raise ActiveRecordError, "cannot update a new record" if new_record?
|
317
665
|
raise ActiveRecordError, "cannot update a destroyed record" if destroyed?
|
318
666
|
|
319
|
-
attributes.
|
320
|
-
|
667
|
+
attributes = attributes.transform_keys do |key|
|
668
|
+
name = key.to_s
|
669
|
+
self.class.attribute_aliases[name] || name
|
321
670
|
end
|
322
671
|
|
323
|
-
|
672
|
+
attributes.each_key do |key|
|
673
|
+
verify_readonly_attribute(key)
|
674
|
+
end
|
324
675
|
|
676
|
+
id_in_database = self.id_in_database
|
325
677
|
attributes.each do |k, v|
|
326
|
-
|
678
|
+
write_attribute_without_type_cast(k, v)
|
327
679
|
end
|
328
680
|
|
329
|
-
|
681
|
+
affected_rows = self.class._update_record(
|
682
|
+
attributes,
|
683
|
+
@primary_key => id_in_database
|
684
|
+
)
|
685
|
+
|
686
|
+
affected_rows == 1
|
330
687
|
end
|
331
688
|
|
332
689
|
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
@@ -341,11 +698,13 @@ module ActiveRecord
|
|
341
698
|
# Wrapper around #increment that writes the update to the database.
|
342
699
|
# Only +attribute+ is updated; the record itself is not saved.
|
343
700
|
# This means that any other modified attributes will still be dirty.
|
344
|
-
# Validations and callbacks are skipped.
|
345
|
-
|
701
|
+
# Validations and callbacks are skipped. Supports the +touch+ option from
|
702
|
+
# +update_counters+, see that for more.
|
703
|
+
# Returns +self+.
|
704
|
+
def increment!(attribute, by = 1, touch: nil)
|
346
705
|
increment(attribute, by)
|
347
|
-
change = public_send(attribute) - (
|
348
|
-
self.class.update_counters(id, attribute => change)
|
706
|
+
change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
|
707
|
+
self.class.update_counters(id, attribute => change, touch: touch)
|
349
708
|
clear_attribute_change(attribute) # eww
|
350
709
|
self
|
351
710
|
end
|
@@ -360,9 +719,11 @@ module ActiveRecord
|
|
360
719
|
# Wrapper around #decrement that writes the update to the database.
|
361
720
|
# Only +attribute+ is updated; the record itself is not saved.
|
362
721
|
# This means that any other modified attributes will still be dirty.
|
363
|
-
# Validations and callbacks are skipped.
|
364
|
-
|
365
|
-
|
722
|
+
# Validations and callbacks are skipped. Supports the +touch+ option from
|
723
|
+
# +update_counters+, see that for more.
|
724
|
+
# Returns +self+.
|
725
|
+
def decrement!(attribute, by = 1, touch: nil)
|
726
|
+
increment!(attribute, -by, touch: touch)
|
366
727
|
end
|
367
728
|
|
368
729
|
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
|
@@ -392,8 +753,8 @@ module ActiveRecord
|
|
392
753
|
|
393
754
|
# Reloads the record from the database.
|
394
755
|
#
|
395
|
-
# This method finds record by its primary key (which could be assigned
|
396
|
-
# modifies the receiver in-place:
|
756
|
+
# This method finds the record by its primary key (which could be assigned
|
757
|
+
# manually) and modifies the receiver in-place:
|
397
758
|
#
|
398
759
|
# account = Account.new
|
399
760
|
# # => #<Account id: nil, email: nil>
|
@@ -448,7 +809,7 @@ module ActiveRecord
|
|
448
809
|
self.class.unscoped { self.class.find(id) }
|
449
810
|
end
|
450
811
|
|
451
|
-
@attributes = fresh_object.instance_variable_get(
|
812
|
+
@attributes = fresh_object.instance_variable_get("@attributes")
|
452
813
|
@new_record = false
|
453
814
|
self
|
454
815
|
end
|
@@ -495,35 +856,14 @@ module ActiveRecord
|
|
495
856
|
MSG
|
496
857
|
end
|
497
858
|
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
unless attributes.empty?
|
503
|
-
changes = {}
|
504
|
-
|
505
|
-
attributes.each do |column|
|
506
|
-
column = column.to_s
|
507
|
-
changes[column] = write_attribute(column, time)
|
508
|
-
end
|
509
|
-
|
510
|
-
clear_attribute_changes(changes.keys)
|
511
|
-
primary_key = self.class.primary_key
|
512
|
-
scope = self.class.unscoped.where(primary_key => _read_attribute(primary_key))
|
859
|
+
attribute_names = timestamp_attributes_for_update_in_model
|
860
|
+
attribute_names |= names.map!(&:to_s).map! { |name|
|
861
|
+
self.class.attribute_aliases[name] || name
|
862
|
+
}
|
513
863
|
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
changes[locking_column] = increment_lock
|
518
|
-
end
|
519
|
-
|
520
|
-
result = scope.update_all(changes) == 1
|
521
|
-
|
522
|
-
if !result && locking_enabled?
|
523
|
-
raise ActiveRecord::StaleObjectError.new(self, "touch")
|
524
|
-
end
|
525
|
-
|
526
|
-
result
|
864
|
+
unless attribute_names.empty?
|
865
|
+
affected_rows = _touch_row(attribute_names, time)
|
866
|
+
@_trigger_update_callback = affected_rows == 1
|
527
867
|
else
|
528
868
|
true
|
529
869
|
end
|
@@ -536,41 +876,65 @@ module ActiveRecord
|
|
536
876
|
end
|
537
877
|
|
538
878
|
def destroy_row
|
539
|
-
|
879
|
+
_delete_row
|
540
880
|
end
|
541
881
|
|
542
|
-
def
|
543
|
-
self.class.
|
882
|
+
def _delete_row
|
883
|
+
self.class._delete_record(@primary_key => id_in_database)
|
544
884
|
end
|
545
885
|
|
546
|
-
def
|
547
|
-
|
548
|
-
|
886
|
+
def _touch_row(attribute_names, time)
|
887
|
+
time ||= current_time_from_proper_timezone
|
888
|
+
|
889
|
+
attribute_names.each do |attr_name|
|
890
|
+
_write_attribute(attr_name, time)
|
891
|
+
end
|
892
|
+
|
893
|
+
_update_row(attribute_names, "touch")
|
894
|
+
end
|
895
|
+
|
896
|
+
def _update_row(attribute_names, attempted_action = "update")
|
897
|
+
self.class._update_record(
|
898
|
+
attributes_with_values(attribute_names),
|
899
|
+
@primary_key => id_in_database
|
900
|
+
)
|
901
|
+
end
|
902
|
+
|
903
|
+
def create_or_update(**, &block)
|
904
|
+
_raise_readonly_record_error if readonly?
|
905
|
+
return false if destroyed?
|
906
|
+
result = new_record? ? _create_record(&block) : _update_record(&block)
|
549
907
|
result != false
|
550
908
|
end
|
551
909
|
|
552
910
|
# Updates the associated record with values matching those of the instance attributes.
|
553
911
|
# Returns the number of affected rows.
|
554
912
|
def _update_record(attribute_names = self.attribute_names)
|
555
|
-
|
556
|
-
|
557
|
-
|
913
|
+
attribute_names = attributes_for_update(attribute_names)
|
914
|
+
|
915
|
+
if attribute_names.empty?
|
916
|
+
affected_rows = 0
|
917
|
+
@_trigger_update_callback = true
|
558
918
|
else
|
559
|
-
|
919
|
+
affected_rows = _update_row(attribute_names)
|
920
|
+
@_trigger_update_callback = affected_rows == 1
|
560
921
|
end
|
561
922
|
|
562
923
|
yield(self) if block_given?
|
563
924
|
|
564
|
-
|
925
|
+
affected_rows
|
565
926
|
end
|
566
927
|
|
567
928
|
# Creates a record with values matching those of the instance attributes
|
568
929
|
# and returns its id.
|
569
930
|
def _create_record(attribute_names = self.attribute_names)
|
570
|
-
|
931
|
+
attribute_names = attributes_for_create(attribute_names)
|
932
|
+
|
933
|
+
new_id = self.class._insert_record(
|
934
|
+
attributes_with_values(attribute_names)
|
935
|
+
)
|
571
936
|
|
572
|
-
new_id
|
573
|
-
self.id ||= new_id if self.class.primary_key
|
937
|
+
self.id ||= new_id if @primary_key
|
574
938
|
|
575
939
|
@new_record = false
|
576
940
|
|
@@ -590,8 +954,14 @@ module ActiveRecord
|
|
590
954
|
@_association_destroy_exception = nil
|
591
955
|
end
|
592
956
|
|
957
|
+
# The name of the method used to touch a +belongs_to+ association when the
|
958
|
+
# +:touch+ option is used.
|
593
959
|
def belongs_to_touch_method
|
594
960
|
:touch
|
595
961
|
end
|
962
|
+
|
963
|
+
def _raise_readonly_record_error
|
964
|
+
raise ReadOnlyRecord, "#{self.class} is marked as readonly"
|
965
|
+
end
|
596
966
|
end
|
597
967
|
end
|