Skip to content

Commit eb1f7cc

Browse files
authored
Merge pull request rails#43446 from sambostock/document-upsert-record-timestamps
Document new `record_timestamps` option on `insert_all`
2 parents ced387e + 3902322 commit eb1f7cc

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Automatically set timestamps on record creation during bulk insert/upsert
2+
3+
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}`).
4+
5+
This behaviour is controlled by the `<model>.record_timestamps` config, matching the behaviour of `create`, `update`, etc. It can also be overridden by using the `record_timestamps:` keyword argument.
6+
7+
Note that this means `upsert_all` on models with `record_timestamps = false` will no longer touch `updated_{at,on}` automatically.
8+
9+
*Sam Bostock*
10+
111
* Don't require `role` when passing `shard` to `connected_to`.
212

313
`connected_to` can now be called with a `shard` only. Note that `role` is still inherited if `connected_to` calls are nested.

activerecord/lib/active_record/persistence.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ def insert(attributes, returning: nil, unique_by: nil, record_timestamps: nil)
110110
# unique_by: %i[ author_id name ]
111111
# unique_by: :index_books_on_isbn
112112
#
113+
# [:record_timestamps]
114+
# By default, automatic setting of timestamp columns is controlled by
115+
# the model's <tt>record_timestamps</tt> config, matching typical
116+
# behavior.
117+
#
118+
# To override this and force automatic setting of timestamp columns one
119+
# way or the other, pass <tt>:record_timestamps</tt>:
120+
#
121+
# record_timestamps: true # Always set timestamps automatically
122+
# record_timestamps: false # Never set timestamps automatically
123+
#
113124
# Because it relies on the index information from the database
114125
# <tt>:unique_by</tt> is recommended to be paired with
115126
# Active Record's schema_cache.
@@ -174,6 +185,17 @@ def insert!(attributes, returning: nil, record_timestamps: nil)
174185
# You can also pass an SQL string if you need more control on the return values
175186
# (for example, <tt>returning: "id, name as new_name"</tt>).
176187
#
188+
# [:record_timestamps]
189+
# By default, automatic setting of timestamp columns is controlled by
190+
# the model's <tt>record_timestamps</tt> config, matching typical
191+
# behavior.
192+
#
193+
# To override this and force automatic setting of timestamp columns one
194+
# way or the other, pass <tt>:record_timestamps</tt>:
195+
#
196+
# record_timestamps: true # Always set timestamps automatically
197+
# record_timestamps: false # Never set timestamps automatically
198+
#
177199
# ==== Examples
178200
#
179201
# # Insert multiple records
@@ -250,6 +272,17 @@ def upsert(attributes, on_duplicate: :update, returning: nil, unique_by: nil, re
250272
#
251273
# NOTE: in this case you must provide all the columns you want to update by yourself.
252274
#
275+
# [:record_timestamps]
276+
# By default, automatic setting of timestamp columns is controlled by
277+
# the model's <tt>record_timestamps</tt> config, matching typical
278+
# behavior.
279+
#
280+
# To override this and force automatic setting of timestamp columns one
281+
# way or the other, pass <tt>:record_timestamps</tt>:
282+
#
283+
# record_timestamps: true # Always set timestamps automatically
284+
# record_timestamps: false # Never set timestamps automatically
285+
#
253286
# ==== Examples
254287
#
255288
# # Inserts multiple records, performing an upsert when records have duplicate ISBNs.

0 commit comments

Comments
 (0)