1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Commit graph

7 commits

Author SHA1 Message Date
Ryuta Kamizono
cad0b7d91d Fix touch option to behave consistently with Persistence#touch method
`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.
2018-06-18 19:08:41 +09:00
bogdanvlviv
6ddba9b46c
Fix conflicts counter_cache with touch: true by optimistic locking.
```
  # create_table :posts do |t|
  #   t.integer :comments_count, default: 0
  #   t.integer :lock_version
  #   t.timestamps
  # end
  class Post < ApplicationRecord
  end

  # create_table :comments do |t|
  #   t.belongs_to :post
  # end
  class Comment < ApplicationRecord
    belongs_to :post, touch: true, counter_cache: true
  end
  ```

  Before:
  ```
  post = Post.create!
  # => begin transaction
       INSERT INTO "posts" ("created_at", "updated_at", "lock_version")
       VALUES ("2017-12-11 21:27:11.387397", "2017-12-11 21:27:11.387397", 0)
       commit transaction

  comment = Comment.create!(post: post)
  # => begin transaction
       INSERT INTO "comments" ("post_id") VALUES (1)

       UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1,
       "lock_version" = COALESCE("lock_version", 0) + 1 WHERE "posts"."id" = 1

       UPDATE "posts" SET "updated_at" = '2017-12-11 21:27:11.398330',
       "lock_version" = 1 WHERE "posts"."id" = 1 AND "posts"."lock_version" = 0
       rollback transaction
  # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: Post.

  Comment.take.destroy!
  # => begin transaction
       DELETE FROM "comments" WHERE "comments"."id" = 1

       UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1,
       "lock_version" = COALESCE("lock_version", 0) + 1 WHERE "posts"."id" = 1

       UPDATE "posts" SET "updated_at" = '2017-12-11 21:42:47.785901',
       "lock_version" = 1 WHERE "posts"."id" = 1 AND "posts"."lock_version" = 0
       rollback transaction
  # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: Post.
  ```

  After:
  ```
  post = Post.create!
  # => begin transaction
       INSERT INTO "posts" ("created_at", "updated_at", "lock_version")
       VALUES ("2017-12-11 21:27:11.387397", "2017-12-11 21:27:11.387397", 0)
       commit transaction

  comment = Comment.create!(post: post)
  # => begin transaction
       INSERT INTO "comments" ("post_id") VALUES (1)

       UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1,
       "lock_version" = COALESCE("lock_version", 0) + 1,
       "updated_at" = '2017-12-11 21:37:09.802642' WHERE "posts"."id" = 1
       commit transaction

  comment.destroy!
  # => begin transaction
       DELETE FROM "comments" WHERE "comments"."id" = 1

       UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1,
       "lock_version" = COALESCE("lock_version", 0) + 1,
       "updated_at" = '2017-12-11 21:39:02.685520' WHERE "posts"."id" = 1
       commit transaction
  ```

  Fixes #31199.
2017-12-12 00:32:50 +02:00
Kir Shatrov
831be98f9a Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590, reversing
changes made to afb66a5a59.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Xavier Noria
d22e522179 modernizes hash syntax in activerecord 2016-08-06 19:37:57 +02:00
Neeraj Singh
1e53404fe9 reset_counter should work with non-traditional belongs_to and polymorphic belongs_to
[#4984 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
2010-07-08 23:24:12 +02:00