diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0f2613910abac..7f556613d11b0 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,13 @@ +* Automatically set timestamps on record creation during bulk insert/upsert + + Prior to this change, only updates during an upsert operation (e.g. `upsert_all`) would touch timestamps (`updated_{at,on}`). Now, record creations also touch timestamp columns (`{created,updated}_{at,on}`). + + This behaviour is controlled by the `.record_timestamps` config, matching the behaviour of `create`, `update`, etc. It can also be overridden by using the `record_timestamps:` keyword argument. + + Note that this means `upsert_all` on models with `record_timestamps = false` will no longer touch `updated_{at,on}` automatically. + + *Sam Bostock* + * Don't require `role` when passing `shard` to `connected_to`. `connected_to` can now be called with a `shard` only. Note that `role` is still inherited if `connected_to` calls are nested. diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 9d04901a69e03..ed5fbe7f9f56a 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -110,6 +110,17 @@ def insert(attributes, returning: nil, unique_by: nil, record_timestamps: nil) # unique_by: %i[ author_id name ] # unique_by: :index_books_on_isbn # + # [:record_timestamps] + # By default, automatic setting of timestamp columns is controlled by + # the model's record_timestamps config, matching typical + # behavior. + # + # To override this and force automatic setting of timestamp columns one + # way or the other, pass :record_timestamps: + # + # record_timestamps: true # Always set timestamps automatically + # record_timestamps: false # Never set timestamps automatically + # # Because it relies on the index information from the database # :unique_by is recommended to be paired with # Active Record's schema_cache. @@ -174,6 +185,17 @@ def insert!(attributes, returning: nil, record_timestamps: nil) # You can also pass an SQL string if you need more control on the return values # (for example, returning: "id, name as new_name"). # + # [:record_timestamps] + # By default, automatic setting of timestamp columns is controlled by + # the model's record_timestamps config, matching typical + # behavior. + # + # To override this and force automatic setting of timestamp columns one + # way or the other, pass :record_timestamps: + # + # record_timestamps: true # Always set timestamps automatically + # record_timestamps: false # Never set timestamps automatically + # # ==== Examples # # # Insert multiple records @@ -250,6 +272,17 @@ def upsert(attributes, on_duplicate: :update, returning: nil, unique_by: nil, re # # NOTE: in this case you must provide all the columns you want to update by yourself. # + # [:record_timestamps] + # By default, automatic setting of timestamp columns is controlled by + # the model's record_timestamps config, matching typical + # behavior. + # + # To override this and force automatic setting of timestamp columns one + # way or the other, pass :record_timestamps: + # + # record_timestamps: true # Always set timestamps automatically + # record_timestamps: false # Never set timestamps automatically + # # ==== Examples # # # Inserts multiple records, performing an upsert when records have duplicate ISBNs.