activerecord 3.2.6 → 6.0.0
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 +7 -0
- data/CHANGELOG.md +611 -6417
- data/MIT-LICENSE +4 -2
- data/README.rdoc +44 -47
- data/examples/performance.rb +79 -71
- data/examples/simple.rb +6 -5
- data/lib/active_record/aggregations.rb +268 -238
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations/alias_tracker.rb +47 -42
- data/lib/active_record/associations/association.rb +173 -81
- data/lib/active_record/associations/association_scope.rb +124 -92
- data/lib/active_record/associations/belongs_to_association.rb +83 -38
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
- data/lib/active_record/associations/builder/association.rb +113 -32
- data/lib/active_record/associations/builder/belongs_to.rb +105 -60
- data/lib/active_record/associations/builder/collection_association.rb +53 -56
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
- data/lib/active_record/associations/builder/has_many.rb +11 -63
- data/lib/active_record/associations/builder/has_one.rb +47 -45
- data/lib/active_record/associations/builder/singular_association.rb +30 -18
- data/lib/active_record/associations/collection_association.rb +217 -295
- data/lib/active_record/associations/collection_proxy.rb +1074 -77
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +78 -50
- data/lib/active_record/associations/has_many_through_association.rb +99 -61
- data/lib/active_record/associations/has_one_association.rb +75 -30
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
- data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
- data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
- data/lib/active_record/associations/join_dependency.rb +208 -164
- data/lib/active_record/associations/preloader/association.rb +93 -87
- data/lib/active_record/associations/preloader/through_association.rb +87 -38
- data/lib/active_record/associations/preloader.rb +134 -110
- data/lib/active_record/associations/singular_association.rb +19 -24
- data/lib/active_record/associations/through_association.rb +61 -27
- data/lib/active_record/associations.rb +1766 -1505
- data/lib/active_record/attribute_assignment.rb +57 -193
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
- data/lib/active_record/attribute_methods/dirty.rb +187 -67
- data/lib/active_record/attribute_methods/primary_key.rb +100 -78
- data/lib/active_record/attribute_methods/query.rb +10 -8
- data/lib/active_record/attribute_methods/read.rb +29 -118
- data/lib/active_record/attribute_methods/serialization.rb +60 -72
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
- data/lib/active_record/attribute_methods/write.rb +36 -44
- data/lib/active_record/attribute_methods.rb +306 -161
- data/lib/active_record/attributes.rb +279 -0
- data/lib/active_record/autosave_association.rb +324 -238
- data/lib/active_record/base.rb +114 -507
- data/lib/active_record/callbacks.rb +147 -83
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +32 -23
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +962 -279
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +510 -289
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
- data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
- data/lib/active_record/connection_adapters/column.rb +58 -233
- data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +200 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +75 -207
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +695 -1052
- data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +103 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- 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 +528 -26
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +267 -0
- data/lib/active_record/core.rb +599 -0
- data/lib/active_record/counter_cache.rb +177 -103
- 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 +107 -64
- data/lib/active_record/enum.rb +274 -0
- data/lib/active_record/errors.rb +254 -61
- data/lib/active_record/explain.rb +35 -70
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +18 -8
- data/lib/active_record/fixture_set/file.rb +82 -0
- 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 +291 -475
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +219 -100
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +175 -17
- data/lib/active_record/internal_metadata.rb +53 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +9 -1
- data/lib/active_record/locking/optimistic.rb +106 -92
- data/lib/active_record/locking/pessimistic.rb +23 -11
- data/lib/active_record/log_subscriber.rb +80 -30
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +235 -56
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/migration.rb +917 -301
- data/lib/active_record/model_schema.rb +351 -175
- data/lib/active_record/nested_attributes.rb +366 -235
- data/lib/active_record/no_touching.rb +65 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +761 -166
- data/lib/active_record/query_cache.rb +22 -44
- data/lib/active_record/querying.rb +55 -31
- data/lib/active_record/railtie.rb +185 -47
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -4
- data/lib/active_record/railties/controller_runtime.rb +35 -33
- data/lib/active_record/railties/databases.rake +366 -463
- data/lib/active_record/readonly_attributes.rb +4 -6
- data/lib/active_record/reflection.rb +736 -228
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +252 -52
- data/lib/active_record/relation/calculations.rb +340 -270
- data/lib/active_record/relation/delegation.rb +117 -36
- data/lib/active_record/relation/finder_methods.rb +439 -286
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +184 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder.rb +131 -39
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +1163 -221
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +49 -120
- data/lib/active_record/relation/where_clause.rb +190 -0
- data/lib/active_record/relation/where_clause_factory.rb +33 -0
- data/lib/active_record/relation.rb +671 -349
- data/lib/active_record/result.rb +149 -15
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +153 -133
- data/lib/active_record/schema.rb +22 -19
- data/lib/active_record/schema_dumper.rb +178 -112
- data/lib/active_record/schema_migration.rb +60 -0
- data/lib/active_record/scoping/default.rb +107 -98
- data/lib/active_record/scoping/named.rb +130 -115
- data/lib/active_record/scoping.rb +77 -123
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +10 -6
- data/lib/active_record/statement_cache.rb +148 -0
- data/lib/active_record/store.rb +256 -16
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +75 -0
- data/lib/active_record/tasks/database_tasks.rb +506 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +93 -39
- data/lib/active_record/touch_later.rb +66 -0
- data/lib/active_record/transactions.rb +260 -129
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +129 -0
- data/lib/active_record/type/date.rb +9 -0
- data/lib/active_record/type/date_time.rb +9 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +71 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +21 -0
- data/lib/active_record/type/type_map.rb +62 -0
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +78 -0
- data/lib/active_record/type_caster/connection.rb +34 -0
- data/lib/active_record/type_caster/map.rb +20 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +35 -18
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +68 -0
- data/lib/active_record/validations/uniqueness.rb +123 -77
- data/lib/active_record/validations.rb +54 -43
- data/lib/active_record/version.rb +7 -7
- data/lib/active_record.rb +97 -49
- 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 +51 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +59 -9
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +48 -0
- data/lib/rails/generators/active_record/migration.rb +41 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +24 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
- data/lib/rails/generators/active_record.rb +10 -16
- metadata +285 -149
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -203
- data/lib/active_record/session_store.rb +0 -358
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,7 +1,9 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/insert_all"
|
2
4
|
|
3
5
|
module ActiveRecord
|
4
|
-
# = Active Record Persistence
|
6
|
+
# = Active Record \Persistence
|
5
7
|
module Persistence
|
6
8
|
extend ActiveSupport::Concern
|
7
9
|
|
@@ -9,99 +11,496 @@ module ActiveRecord
|
|
9
11
|
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
|
10
12
|
# The resulting object is returned whether the object was saved successfully to the database or not.
|
11
13
|
#
|
12
|
-
# The +attributes+ parameter can be either
|
14
|
+
# The +attributes+ parameter can be either a Hash or an Array of Hashes. These Hashes describe the
|
13
15
|
# attributes on the objects that are to be created.
|
14
16
|
#
|
15
|
-
# +create+ respects mass-assignment security and accepts either +:as+ or +:without_protection+ options
|
16
|
-
# in the +options+ parameter.
|
17
|
-
#
|
18
17
|
# ==== Examples
|
19
18
|
# # Create a single new object
|
20
|
-
# User.create(:
|
21
|
-
#
|
22
|
-
# # Create a single new object using the :admin mass-assignment security role
|
23
|
-
# User.create({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
|
24
|
-
#
|
25
|
-
# # Create a single new object bypassing mass-assignment security
|
26
|
-
# User.create({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
|
19
|
+
# User.create(first_name: 'Jamie')
|
27
20
|
#
|
28
21
|
# # Create an Array of new objects
|
29
|
-
# User.create([{ :
|
22
|
+
# User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])
|
30
23
|
#
|
31
24
|
# # Create a single object and pass it into a block to set other attributes.
|
32
|
-
# User.create(:
|
25
|
+
# User.create(first_name: 'Jamie') do |u|
|
33
26
|
# u.is_admin = false
|
34
27
|
# end
|
35
28
|
#
|
36
29
|
# # Creating an Array of new objects using a block, where the block is executed for each object:
|
37
|
-
# User.create([{ :
|
30
|
+
# User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
|
38
31
|
# u.is_admin = false
|
39
32
|
# end
|
40
|
-
def create(attributes = nil,
|
33
|
+
def create(attributes = nil, &block)
|
41
34
|
if attributes.is_a?(Array)
|
42
|
-
attributes.collect { |attr| create(attr,
|
35
|
+
attributes.collect { |attr| create(attr, &block) }
|
43
36
|
else
|
44
|
-
object = new(attributes,
|
37
|
+
object = new(attributes, &block)
|
45
38
|
object.save
|
46
39
|
object
|
47
40
|
end
|
48
41
|
end
|
42
|
+
|
43
|
+
# Creates an object (or multiple objects) and saves it to the database,
|
44
|
+
# if validations pass. Raises a RecordInvalid error if validations fail,
|
45
|
+
# unlike Base#create.
|
46
|
+
#
|
47
|
+
# The +attributes+ parameter can be either a Hash or an Array of Hashes.
|
48
|
+
# These describe which attributes to be created on the object, or
|
49
|
+
# multiple objects when given an Array of Hashes.
|
50
|
+
def create!(attributes = nil, &block)
|
51
|
+
if attributes.is_a?(Array)
|
52
|
+
attributes.collect { |attr| create!(attr, &block) }
|
53
|
+
else
|
54
|
+
object = new(attributes, &block)
|
55
|
+
object.save!
|
56
|
+
object
|
57
|
+
end
|
58
|
+
end
|
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
|
+
|
246
|
+
# Given an attributes hash, +instantiate+ returns a new instance of
|
247
|
+
# the appropriate class. Accepts only keys as strings.
|
248
|
+
#
|
249
|
+
# For example, +Post.all+ may return Comments, Messages, and Emails
|
250
|
+
# by storing the record's subclass in a +type+ attribute. By calling
|
251
|
+
# +instantiate+ instead of +new+, finder methods ensure they get new
|
252
|
+
# instances of the appropriate class for each record.
|
253
|
+
#
|
254
|
+
# See <tt>ActiveRecord::Inheritance#discriminate_class_for_record</tt> to see
|
255
|
+
# how this "single-table" inheritance mapping is implemented.
|
256
|
+
def instantiate(attributes, column_types = {}, &block)
|
257
|
+
klass = discriminate_class_for_record(attributes)
|
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")
|
396
|
+
end
|
397
|
+
|
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
|
+
|
406
|
+
# Called by +instantiate+ to decide which class to use for a new
|
407
|
+
# record instance.
|
408
|
+
#
|
409
|
+
# See +ActiveRecord::Inheritance#discriminate_class_for_record+ for
|
410
|
+
# the single-table inheritance discriminator.
|
411
|
+
def discriminate_class_for_record(record)
|
412
|
+
self
|
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
|
49
422
|
end
|
50
423
|
|
51
424
|
# Returns true if this object hasn't been saved yet -- that is, a record
|
52
|
-
# for the object doesn't exist in the
|
425
|
+
# for the object doesn't exist in the database yet; otherwise, returns false.
|
53
426
|
def new_record?
|
427
|
+
sync_with_transaction_state if @transaction_state&.finalized?
|
54
428
|
@new_record
|
55
429
|
end
|
56
430
|
|
57
431
|
# Returns true if this object has been destroyed, otherwise returns false.
|
58
432
|
def destroyed?
|
433
|
+
sync_with_transaction_state if @transaction_state&.finalized?
|
59
434
|
@destroyed
|
60
435
|
end
|
61
436
|
|
62
|
-
# Returns if the record is persisted, i.e. it's not a new record and it was
|
63
|
-
# not destroyed.
|
437
|
+
# Returns true if the record is persisted, i.e. it's not a new record and it was
|
438
|
+
# not destroyed, otherwise returns false.
|
64
439
|
def persisted?
|
65
|
-
|
440
|
+
sync_with_transaction_state if @transaction_state&.finalized?
|
441
|
+
!(@new_record || @destroyed)
|
66
442
|
end
|
67
443
|
|
444
|
+
##
|
445
|
+
# :call-seq:
|
446
|
+
# save(*args)
|
447
|
+
#
|
68
448
|
# Saves the model.
|
69
449
|
#
|
70
|
-
# If the model is new a record gets created in the database, otherwise
|
450
|
+
# If the model is new, a record gets created in the database, otherwise
|
71
451
|
# the existing record gets updated.
|
72
452
|
#
|
73
|
-
# By default, save always
|
74
|
-
# is cancelled and
|
75
|
-
# :
|
453
|
+
# By default, save always runs validations. If any of them fail the action
|
454
|
+
# is cancelled and #save returns +false+, and the record won't be saved. However, if you supply
|
455
|
+
# <tt>validate: false</tt>, validations are bypassed altogether. See
|
76
456
|
# ActiveRecord::Validations for more information.
|
77
457
|
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
458
|
+
# By default, #save also sets the +updated_at+/+updated_on+ attributes to
|
459
|
+
# the current time. However, if you supply <tt>touch: false</tt>, these
|
460
|
+
# timestamps will not be updated.
|
461
|
+
#
|
462
|
+
# There's a series of callbacks associated with #save. If any of the
|
463
|
+
# <tt>before_*</tt> callbacks throws +:abort+ the action is cancelled and
|
464
|
+
# #save returns +false+. See ActiveRecord::Callbacks for further
|
81
465
|
# details.
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
466
|
+
#
|
467
|
+
# Attributes marked as readonly are silently ignored if the record is
|
468
|
+
# being updated.
|
469
|
+
def save(*args, &block)
|
470
|
+
create_or_update(*args, &block)
|
471
|
+
rescue ActiveRecord::RecordInvalid
|
472
|
+
false
|
88
473
|
end
|
89
474
|
|
475
|
+
##
|
476
|
+
# :call-seq:
|
477
|
+
# save!(*args)
|
478
|
+
#
|
90
479
|
# Saves the model.
|
91
480
|
#
|
92
|
-
# If the model is new a record gets created in the database, otherwise
|
481
|
+
# If the model is new, a record gets created in the database, otherwise
|
93
482
|
# the existing record gets updated.
|
94
483
|
#
|
95
|
-
#
|
96
|
-
# ActiveRecord::RecordInvalid gets raised.
|
97
|
-
#
|
484
|
+
# By default, #save! always runs validations. If any of them fail
|
485
|
+
# ActiveRecord::RecordInvalid gets raised, and the record won't be saved. However, if you supply
|
486
|
+
# <tt>validate: false</tt>, validations are bypassed altogether. See
|
487
|
+
# ActiveRecord::Validations for more information.
|
488
|
+
#
|
489
|
+
# By default, #save! also sets the +updated_at+/+updated_on+ attributes to
|
490
|
+
# the current time. However, if you supply <tt>touch: false</tt>, these
|
491
|
+
# timestamps will not be updated.
|
98
492
|
#
|
99
|
-
# There's a series of callbacks associated with
|
100
|
-
# the <tt>before_*</tt> callbacks
|
101
|
-
# and
|
493
|
+
# There's a series of callbacks associated with #save!. If any of
|
494
|
+
# the <tt>before_*</tt> callbacks throws +:abort+ the action is cancelled
|
495
|
+
# and #save! raises ActiveRecord::RecordNotSaved. See
|
102
496
|
# ActiveRecord::Callbacks for further details.
|
103
|
-
|
104
|
-
|
497
|
+
#
|
498
|
+
# Attributes marked as readonly are silently ignored if the record is
|
499
|
+
# being updated.
|
500
|
+
#
|
501
|
+
# Unless an error is raised, returns true.
|
502
|
+
def save!(*args, &block)
|
503
|
+
create_or_update(*args, &block) || raise(RecordNotSaved.new("Failed to save the record", self))
|
105
504
|
end
|
106
505
|
|
107
506
|
# Deletes the record in the database and freezes this instance to
|
@@ -111,58 +510,83 @@ module ActiveRecord
|
|
111
510
|
# The row is simply removed with an SQL +DELETE+ statement on the
|
112
511
|
# record's primary key, and no callbacks are executed.
|
113
512
|
#
|
513
|
+
# Note that this will also delete records marked as {#readonly?}[rdoc-ref:Core#readonly?].
|
514
|
+
#
|
114
515
|
# To enforce the object's +before_destroy+ and +after_destroy+
|
115
|
-
# callbacks
|
516
|
+
# callbacks or any <tt>:dependent</tt> association
|
116
517
|
# options, use <tt>#destroy</tt>.
|
117
518
|
def delete
|
118
|
-
if persisted?
|
119
|
-
self.class.delete(id)
|
120
|
-
IdentityMap.remove(self) if IdentityMap.enabled?
|
121
|
-
end
|
519
|
+
_delete_row if persisted?
|
122
520
|
@destroyed = true
|
123
521
|
freeze
|
124
522
|
end
|
125
523
|
|
126
524
|
# Deletes the record in the database and freezes this instance to reflect
|
127
525
|
# that no changes should be made (since they can't be persisted).
|
526
|
+
#
|
527
|
+
# There's a series of callbacks associated with #destroy. If the
|
528
|
+
# <tt>before_destroy</tt> callback throws +:abort+ the action is cancelled
|
529
|
+
# and #destroy returns +false+.
|
530
|
+
# See ActiveRecord::Callbacks for further details.
|
128
531
|
def destroy
|
532
|
+
_raise_readonly_record_error if readonly?
|
129
533
|
destroy_associations
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
column = self.class.columns_hash[pk]
|
135
|
-
substitute = connection.substitute_at(column, 0)
|
136
|
-
|
137
|
-
relation = self.class.unscoped.where(
|
138
|
-
self.class.arel_table[pk].eq(substitute))
|
139
|
-
|
140
|
-
relation.bind_values = [[column, id]]
|
141
|
-
relation.delete_all
|
534
|
+
@_trigger_destroy_callback = if persisted?
|
535
|
+
destroy_row > 0
|
536
|
+
else
|
537
|
+
true
|
142
538
|
end
|
143
|
-
|
144
539
|
@destroyed = true
|
145
540
|
freeze
|
146
541
|
end
|
147
542
|
|
543
|
+
# Deletes the record in the database and freezes this instance to reflect
|
544
|
+
# that no changes should be made (since they can't be persisted).
|
545
|
+
#
|
546
|
+
# There's a series of callbacks associated with #destroy!. If the
|
547
|
+
# <tt>before_destroy</tt> callback throws +:abort+ the action is cancelled
|
548
|
+
# and #destroy! raises ActiveRecord::RecordNotDestroyed.
|
549
|
+
# See ActiveRecord::Callbacks for further details.
|
550
|
+
def destroy!
|
551
|
+
destroy || _raise_record_not_destroyed
|
552
|
+
end
|
553
|
+
|
148
554
|
# Returns an instance of the specified +klass+ with the attributes of the
|
149
555
|
# current record. This is mostly useful in relation to single-table
|
150
556
|
# inheritance structures where you want a subclass to appear as the
|
151
557
|
# superclass. This can be used along with record identification in
|
152
558
|
# Action Pack to allow, say, <tt>Client < Company</tt> to do something
|
153
|
-
# like render <tt
|
559
|
+
# like render <tt>partial: @client.becomes(Company)</tt> to render that
|
154
560
|
# instance using the companies/company partial instead of clients/client.
|
155
561
|
#
|
156
562
|
# Note: The new instance will share a link to the same attributes as the original class.
|
157
|
-
#
|
563
|
+
# Therefore the sti column value will still be the same.
|
564
|
+
# Any change to the attributes on either instance will affect both instances.
|
565
|
+
# If you want to change the sti column as well, use #becomes! instead.
|
158
566
|
def becomes(klass)
|
159
|
-
became = klass.
|
567
|
+
became = klass.allocate
|
568
|
+
became.send(:initialize)
|
160
569
|
became.instance_variable_set("@attributes", @attributes)
|
161
|
-
became.instance_variable_set("@
|
570
|
+
became.instance_variable_set("@mutations_from_database", @mutations_from_database ||= nil)
|
162
571
|
became.instance_variable_set("@new_record", new_record?)
|
163
572
|
became.instance_variable_set("@destroyed", destroyed?)
|
164
|
-
became.
|
165
|
-
became
|
573
|
+
became.errors.copy!(errors)
|
574
|
+
became
|
575
|
+
end
|
576
|
+
|
577
|
+
# Wrapper around #becomes that also changes the instance's sti column value.
|
578
|
+
# This is especially useful if you want to persist the changed class in your
|
579
|
+
# database.
|
580
|
+
#
|
581
|
+
# Note: The old instance's sti column value will be changed too, as both objects
|
582
|
+
# share the same set of attributes.
|
583
|
+
def becomes!(klass)
|
584
|
+
became = becomes(klass)
|
585
|
+
sti_type = nil
|
586
|
+
if !klass.descends_from_active_record?
|
587
|
+
sti_type = klass.sti_name
|
588
|
+
end
|
589
|
+
became.public_send("#{klass.inheritance_column}=", sti_type)
|
166
590
|
became
|
167
591
|
end
|
168
592
|
|
@@ -170,61 +594,98 @@ module ActiveRecord
|
|
170
594
|
# This is especially useful for boolean flags on existing records. Also note that
|
171
595
|
#
|
172
596
|
# * Validation is skipped.
|
173
|
-
# * Callbacks are invoked.
|
597
|
+
# * \Callbacks are invoked.
|
174
598
|
# * updated_at/updated_on column is updated if that column is available.
|
175
599
|
# * Updates all the attributes that are dirty in this object.
|
176
600
|
#
|
601
|
+
# This method raises an ActiveRecord::ActiveRecordError if the
|
602
|
+
# attribute is marked as readonly.
|
603
|
+
#
|
604
|
+
# Also see #update_column.
|
177
605
|
def update_attribute(name, value)
|
178
606
|
name = name.to_s
|
179
|
-
|
180
|
-
|
181
|
-
save(:validate => false)
|
182
|
-
end
|
607
|
+
verify_readonly_attribute(name)
|
608
|
+
public_send("#{name}=", value)
|
183
609
|
|
184
|
-
|
185
|
-
#
|
186
|
-
# * Validation is skipped.
|
187
|
-
# * Callbacks are skipped.
|
188
|
-
# * updated_at/updated_on column is not updated if that column is available.
|
189
|
-
#
|
190
|
-
# Raises an +ActiveRecordError+ when called on new objects, or when the +name+
|
191
|
-
# attribute is marked as readonly.
|
192
|
-
def update_column(name, value)
|
193
|
-
name = name.to_s
|
194
|
-
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
|
195
|
-
raise ActiveRecordError, "can not update on a new record object" unless persisted?
|
196
|
-
raw_write_attribute(name, value)
|
197
|
-
self.class.update_all({ name => value }, self.class.primary_key => id) == 1
|
610
|
+
save(validate: false)
|
198
611
|
end
|
199
612
|
|
200
613
|
# Updates the attributes of the model from the passed-in hash and saves the
|
201
614
|
# record, all wrapped in a transaction. If the object is invalid, the saving
|
202
615
|
# will fail and false will be returned.
|
203
|
-
|
204
|
-
# When updating model attributes, mass-assignment security protection is respected.
|
205
|
-
# If no +:as+ option is supplied then the +:default+ role will be used.
|
206
|
-
# If you want to bypass the protection given by +attr_protected+ and
|
207
|
-
# +attr_accessible+ then you can do so using the +:without_protection+ option.
|
208
|
-
def update_attributes(attributes, options = {})
|
616
|
+
def update(attributes)
|
209
617
|
# The following transaction covers any possible database side-effects of the
|
210
618
|
# attributes assignment. For example, setting the IDs of a child collection.
|
211
619
|
with_transaction_returning_status do
|
212
|
-
|
620
|
+
assign_attributes(attributes)
|
213
621
|
save
|
214
622
|
end
|
215
623
|
end
|
216
624
|
|
217
|
-
|
218
|
-
|
219
|
-
|
625
|
+
alias update_attributes update
|
626
|
+
deprecate update_attributes: "please, use update instead"
|
627
|
+
|
628
|
+
# Updates its receiver just like #update but calls #save! instead
|
629
|
+
# of +save+, so an exception is raised if the record is invalid and saving will fail.
|
630
|
+
def update!(attributes)
|
220
631
|
# The following transaction covers any possible database side-effects of the
|
221
632
|
# attributes assignment. For example, setting the IDs of a child collection.
|
222
633
|
with_transaction_returning_status do
|
223
|
-
|
634
|
+
assign_attributes(attributes)
|
224
635
|
save!
|
225
636
|
end
|
226
637
|
end
|
227
638
|
|
639
|
+
alias update_attributes! update!
|
640
|
+
deprecate update_attributes!: "please, use update! instead"
|
641
|
+
|
642
|
+
# Equivalent to <code>update_columns(name => value)</code>.
|
643
|
+
def update_column(name, value)
|
644
|
+
update_columns(name => value)
|
645
|
+
end
|
646
|
+
|
647
|
+
# Updates the attributes directly in the database issuing an UPDATE SQL
|
648
|
+
# statement and sets them in the receiver:
|
649
|
+
#
|
650
|
+
# user.update_columns(last_request_at: Time.current)
|
651
|
+
#
|
652
|
+
# This is the fastest way to update attributes because it goes straight to
|
653
|
+
# the database, but take into account that in consequence the regular update
|
654
|
+
# procedures are totally bypassed. In particular:
|
655
|
+
#
|
656
|
+
# * \Validations are skipped.
|
657
|
+
# * \Callbacks are skipped.
|
658
|
+
# * +updated_at+/+updated_on+ are not updated.
|
659
|
+
# * However, attributes are serialized with the same rules as ActiveRecord::Relation#update_all
|
660
|
+
#
|
661
|
+
# This method raises an ActiveRecord::ActiveRecordError when called on new
|
662
|
+
# objects, or when at least one of the attributes is marked as readonly.
|
663
|
+
def update_columns(attributes)
|
664
|
+
raise ActiveRecordError, "cannot update a new record" if new_record?
|
665
|
+
raise ActiveRecordError, "cannot update a destroyed record" if destroyed?
|
666
|
+
|
667
|
+
attributes = attributes.transform_keys do |key|
|
668
|
+
name = key.to_s
|
669
|
+
self.class.attribute_aliases[name] || name
|
670
|
+
end
|
671
|
+
|
672
|
+
attributes.each_key do |key|
|
673
|
+
verify_readonly_attribute(key)
|
674
|
+
end
|
675
|
+
|
676
|
+
id_in_database = self.id_in_database
|
677
|
+
attributes.each do |k, v|
|
678
|
+
write_attribute_without_type_cast(k, v)
|
679
|
+
end
|
680
|
+
|
681
|
+
affected_rows = self.class._update_record(
|
682
|
+
attributes,
|
683
|
+
@primary_key => id_in_database
|
684
|
+
)
|
685
|
+
|
686
|
+
affected_rows == 1
|
687
|
+
end
|
688
|
+
|
228
689
|
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
|
229
690
|
# The increment is performed directly on the underlying attribute, no setter is invoked.
|
230
691
|
# Only makes sense for number-based attributes. Returns +self+.
|
@@ -234,102 +695,177 @@ module ActiveRecord
|
|
234
695
|
self
|
235
696
|
end
|
236
697
|
|
237
|
-
# Wrapper around
|
238
|
-
#
|
239
|
-
#
|
240
|
-
#
|
241
|
-
|
242
|
-
|
698
|
+
# Wrapper around #increment that writes the update to the database.
|
699
|
+
# Only +attribute+ is updated; the record itself is not saved.
|
700
|
+
# This means that any other modified attributes will still be dirty.
|
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)
|
705
|
+
increment(attribute, by)
|
706
|
+
change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
|
707
|
+
self.class.update_counters(id, attribute => change, touch: touch)
|
708
|
+
clear_attribute_change(attribute) # eww
|
709
|
+
self
|
243
710
|
end
|
244
711
|
|
245
712
|
# Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
|
246
713
|
# The decrement is performed directly on the underlying attribute, no setter is invoked.
|
247
714
|
# Only makes sense for number-based attributes. Returns +self+.
|
248
715
|
def decrement(attribute, by = 1)
|
249
|
-
|
250
|
-
self[attribute] -= by
|
251
|
-
self
|
716
|
+
increment(attribute, -by)
|
252
717
|
end
|
253
718
|
|
254
|
-
# Wrapper around
|
255
|
-
#
|
256
|
-
#
|
257
|
-
#
|
258
|
-
|
259
|
-
|
719
|
+
# Wrapper around #decrement that writes the update to the database.
|
720
|
+
# Only +attribute+ is updated; the record itself is not saved.
|
721
|
+
# This means that any other modified attributes will still be dirty.
|
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)
|
260
727
|
end
|
261
728
|
|
262
729
|
# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
|
263
730
|
# if the predicate returns +true+ the attribute will become +false+. This
|
264
731
|
# method toggles directly the underlying value without calling any setter.
|
265
732
|
# Returns +self+.
|
733
|
+
#
|
734
|
+
# Example:
|
735
|
+
#
|
736
|
+
# user = User.first
|
737
|
+
# user.banned? # => false
|
738
|
+
# user.toggle(:banned)
|
739
|
+
# user.banned? # => true
|
740
|
+
#
|
266
741
|
def toggle(attribute)
|
267
|
-
self[attribute] = !
|
742
|
+
self[attribute] = !public_send("#{attribute}?")
|
268
743
|
self
|
269
744
|
end
|
270
745
|
|
271
|
-
# Wrapper around
|
272
|
-
# its non-bang version in that it passes through the attribute setter.
|
746
|
+
# Wrapper around #toggle that saves the record. This method differs from
|
747
|
+
# its non-bang version in the sense that it passes through the attribute setter.
|
273
748
|
# Saving is not subjected to validation checks. Returns +true+ if the
|
274
749
|
# record could be saved.
|
275
750
|
def toggle!(attribute)
|
276
751
|
toggle(attribute).update_attribute(attribute, self[attribute])
|
277
752
|
end
|
278
753
|
|
279
|
-
# Reloads the
|
280
|
-
#
|
281
|
-
#
|
282
|
-
#
|
754
|
+
# Reloads the record from the database.
|
755
|
+
#
|
756
|
+
# This method finds the record by its primary key (which could be assigned
|
757
|
+
# manually) and modifies the receiver in-place:
|
758
|
+
#
|
759
|
+
# account = Account.new
|
760
|
+
# # => #<Account id: nil, email: nil>
|
761
|
+
# account.id = 1
|
762
|
+
# account.reload
|
763
|
+
# # Account Load (1.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT 1 [["id", 1]]
|
764
|
+
# # => #<Account id: 1, email: 'account@example.com'>
|
765
|
+
#
|
766
|
+
# Attributes are reloaded from the database, and caches busted, in
|
767
|
+
# particular the associations cache and the QueryCache.
|
768
|
+
#
|
769
|
+
# If the record no longer exists in the database ActiveRecord::RecordNotFound
|
770
|
+
# is raised. Otherwise, in addition to the in-place modification the method
|
771
|
+
# returns +self+ for convenience.
|
772
|
+
#
|
773
|
+
# The optional <tt>:lock</tt> flag option allows you to lock the reloaded record:
|
774
|
+
#
|
775
|
+
# reload(lock: true) # reload with pessimistic locking
|
776
|
+
#
|
777
|
+
# Reloading is commonly used in test suites to test something is actually
|
778
|
+
# written to the database, or when some action modifies the corresponding
|
779
|
+
# row in the database but not the object in memory:
|
780
|
+
#
|
781
|
+
# assert account.deposit!(25)
|
782
|
+
# assert_equal 25, account.credit # check it is updated in memory
|
783
|
+
# assert_equal 25, account.reload.credit # check it is also persisted
|
784
|
+
#
|
785
|
+
# Another common use case is optimistic locking handling:
|
786
|
+
#
|
787
|
+
# def with_optimistic_retry
|
788
|
+
# begin
|
789
|
+
# yield
|
790
|
+
# rescue ActiveRecord::StaleObjectError
|
791
|
+
# begin
|
792
|
+
# # Reload lock_version in particular.
|
793
|
+
# reload
|
794
|
+
# rescue ActiveRecord::RecordNotFound
|
795
|
+
# # If the record is gone there is nothing to do.
|
796
|
+
# else
|
797
|
+
# retry
|
798
|
+
# end
|
799
|
+
# end
|
800
|
+
# end
|
801
|
+
#
|
283
802
|
def reload(options = nil)
|
284
|
-
|
285
|
-
clear_association_cache
|
803
|
+
self.class.connection.clear_query_cache
|
286
804
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
805
|
+
fresh_object =
|
806
|
+
if options && options[:lock]
|
807
|
+
self.class.unscoped { self.class.lock(options[:lock]).find(id) }
|
808
|
+
else
|
809
|
+
self.class.unscoped { self.class.find(id) }
|
810
|
+
end
|
291
811
|
|
292
|
-
@
|
812
|
+
@attributes = fresh_object.instance_variable_get("@attributes")
|
813
|
+
@new_record = false
|
293
814
|
self
|
294
815
|
end
|
295
816
|
|
296
|
-
# Saves the record with the updated_at/on attributes set to the current time
|
297
|
-
#
|
298
|
-
#
|
299
|
-
#
|
817
|
+
# Saves the record with the updated_at/on attributes set to the current time
|
818
|
+
# or the time specified.
|
819
|
+
# Please note that no validation is performed and only the +after_touch+,
|
820
|
+
# +after_commit+ and +after_rollback+ callbacks are executed.
|
300
821
|
#
|
301
|
-
#
|
302
|
-
#
|
822
|
+
# This method can be passed attribute names and an optional time argument.
|
823
|
+
# If attribute names are passed, they are updated along with updated_at/on
|
824
|
+
# attributes. If no time argument is passed, the current time is used as default.
|
303
825
|
#
|
304
|
-
#
|
826
|
+
# product.touch # updates updated_at/on with current time
|
827
|
+
# product.touch(time: Time.new(2015, 2, 16, 0, 0, 0)) # updates updated_at/on with specified time
|
828
|
+
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
|
829
|
+
# product.touch(:started_at, :ended_at) # updates started_at, ended_at and updated_at/on attributes
|
830
|
+
#
|
831
|
+
# If used along with {belongs_to}[rdoc-ref:Associations::ClassMethods#belongs_to]
|
832
|
+
# then +touch+ will invoke +touch+ method on associated object.
|
305
833
|
#
|
306
834
|
# class Brake < ActiveRecord::Base
|
307
|
-
# belongs_to :car, :
|
835
|
+
# belongs_to :car, touch: true
|
308
836
|
# end
|
309
837
|
#
|
310
838
|
# class Car < ActiveRecord::Base
|
311
|
-
# belongs_to :corporation, :
|
839
|
+
# belongs_to :corporation, touch: true
|
312
840
|
# end
|
313
841
|
#
|
314
842
|
# # triggers @brake.car.touch and @brake.car.corporation.touch
|
315
843
|
# @brake.touch
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
844
|
+
#
|
845
|
+
# Note that +touch+ must be used on a persisted object, or else an
|
846
|
+
# ActiveRecordError will be thrown. For example:
|
847
|
+
#
|
848
|
+
# ball = Ball.new
|
849
|
+
# ball.touch(:updated_at) # => raises ActiveRecordError
|
850
|
+
#
|
851
|
+
def touch(*names, time: nil)
|
852
|
+
unless persisted?
|
853
|
+
raise ActiveRecordError, <<-MSG.squish
|
854
|
+
cannot touch on a new or destroyed record object. Consider using
|
855
|
+
persisted?, new_record?, or destroyed? before touching
|
856
|
+
MSG
|
857
|
+
end
|
327
858
|
|
328
|
-
|
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
|
+
}
|
329
863
|
|
330
|
-
|
331
|
-
|
332
|
-
|
864
|
+
unless attribute_names.empty?
|
865
|
+
affected_rows = _touch_row(attribute_names, time)
|
866
|
+
@_trigger_update_callback = affected_rows == 1
|
867
|
+
else
|
868
|
+
true
|
333
869
|
end
|
334
870
|
end
|
335
871
|
|
@@ -339,34 +875,93 @@ module ActiveRecord
|
|
339
875
|
def destroy_associations
|
340
876
|
end
|
341
877
|
|
342
|
-
def
|
343
|
-
|
344
|
-
|
878
|
+
def destroy_row
|
879
|
+
_delete_row
|
880
|
+
end
|
881
|
+
|
882
|
+
def _delete_row
|
883
|
+
self.class._delete_record(@primary_key => id_in_database)
|
884
|
+
end
|
885
|
+
|
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)
|
345
907
|
result != false
|
346
908
|
end
|
347
909
|
|
348
910
|
# Updates the associated record with values matching those of the instance attributes.
|
349
911
|
# Returns the number of affected rows.
|
350
|
-
def
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
912
|
+
def _update_record(attribute_names = self.attribute_names)
|
913
|
+
attribute_names = attributes_for_update(attribute_names)
|
914
|
+
|
915
|
+
if attribute_names.empty?
|
916
|
+
affected_rows = 0
|
917
|
+
@_trigger_update_callback = true
|
918
|
+
else
|
919
|
+
affected_rows = _update_row(attribute_names)
|
920
|
+
@_trigger_update_callback = affected_rows == 1
|
921
|
+
end
|
922
|
+
|
923
|
+
yield(self) if block_given?
|
924
|
+
|
925
|
+
affected_rows
|
356
926
|
end
|
357
927
|
|
358
928
|
# Creates a record with values matching those of the instance attributes
|
359
929
|
# and returns its id.
|
360
|
-
def
|
361
|
-
|
930
|
+
def _create_record(attribute_names = self.attribute_names)
|
931
|
+
attribute_names = attributes_for_create(attribute_names)
|
362
932
|
|
363
|
-
new_id = self.class.
|
933
|
+
new_id = self.class._insert_record(
|
934
|
+
attributes_with_values(attribute_names)
|
935
|
+
)
|
364
936
|
|
365
|
-
self.id ||= new_id if
|
937
|
+
self.id ||= new_id if @primary_key
|
366
938
|
|
367
|
-
IdentityMap.add(self) if IdentityMap.enabled?
|
368
939
|
@new_record = false
|
940
|
+
|
941
|
+
yield(self) if block_given?
|
942
|
+
|
369
943
|
id
|
370
944
|
end
|
945
|
+
|
946
|
+
def verify_readonly_attribute(name)
|
947
|
+
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
|
948
|
+
end
|
949
|
+
|
950
|
+
def _raise_record_not_destroyed
|
951
|
+
@_association_destroy_exception ||= nil
|
952
|
+
raise @_association_destroy_exception || RecordNotDestroyed.new("Failed to destroy the record", self)
|
953
|
+
ensure
|
954
|
+
@_association_destroy_exception = nil
|
955
|
+
end
|
956
|
+
|
957
|
+
# The name of the method used to touch a +belongs_to+ association when the
|
958
|
+
# +:touch+ option is used.
|
959
|
+
def belongs_to_touch_method
|
960
|
+
:touch
|
961
|
+
end
|
962
|
+
|
963
|
+
def _raise_readonly_record_error
|
964
|
+
raise ReadOnlyRecord, "#{self.class} is marked as readonly"
|
965
|
+
end
|
371
966
|
end
|
372
967
|
end
|