Repository navigation
Add the touch option to ActiveRecord#increment! and decrement! - #27660
Conversation
|
r? @kaspth (@rails-bot has picked a reviewer for you, use r? to override) |
kaspth
left a comment
There was a problem hiding this comment.
Once the changes have been made and a changelog entry in Active Record's changelog is added, you need to squash your commits down to 1. Thanks!
There was a problem hiding this comment.
Why do we need this change? Presumably sanitize_sql_for_assignment returns an empty string that's safe to append?
There was a problem hiding this comment.
if the last item of updates is an empty string, ActiveRecord generates an SQL like below.
updates = ["\"credit_limit\" = COALESCE(\"credit_limit\", 0) + 1", ""]
unscoped.where(primary_key => id).update_all updates.join(", ")
# => UPDATE "accounts" SET "credit_limit" = COALESCE("credit_limit", 0) + 1, WHERE "accounts"."id" = ?This causes ActiveRecord::StatementInvalid.
There was a problem hiding this comment.
Needs to collect the touch argument from the user and pass it on like this:
# Wrapper around #increment that writes the update to the database.
# Only +attribute+ is updated; the record itself is not saved.
# This means that any other modified attributes will still be dirty.
# Validations and callbacks are skipped. Supports the `touch` option from
# +update_counters+, see that for more.
# Returns +self+.
def increment!(attribute, by = 1, touch: nil)
# …
self.class.update_counters(id, attribute => change, touch: touch)
# …
endSame equivalent for decrement! and a test there.
Supports the `touch` option from update_counters. The default behavior is not to update timestamp columns.
0427f63 to
bad9bfb
Compare
|
Neat, thanks! |
…thod `touch` option was added to `increment!` (#27660) and `update_counters` (#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, #31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
…thod `touch` option was added to `increment!` (rails#27660) and `update_counters` (rails#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, rails#31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
Summary
I modified #21979 with the changes from #26995.
In this commit, AR#increment! updates timestamp columns.
This is the same behavior as Rails before #11410