New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In after_touch, incorrect saved_changes #33429
Comments
I do not know what this behavior was guaranteed, Dirty uses rails/activerecord/lib/active_record/attribute_methods/dirty.rb Lines 19 to 20 in bd01f98
But Was this working with the old version of Rails? |
Hello Yaginuma-san, thank you for your response.
I think But,
For example, the @sgrif what do you think? |
Thank you for your comment. Since diff --git a/33429.rb b/33429.rb
index 422bafd..753b54e 100644
--- a/33429.rb
+++ b/33429.rb
@@ -45,6 +45,7 @@ end
class BugTest < Minitest::Test
def test_saved_changes
post = Post.create!
+ post.reload
post.touch
assert_equal %w[updated_at], $after_touch_saved_changes.keys
(I also want to wait for the other maintainer's comment ;) |
Oh! I did not realize the changes were coming from the Maybe we just need to mention in the documentation, something like: # Saves the record with the updated_at/on attributes set to the current time
# or the time specified.
# Please note that no validation is performed and only the +after_touch+,
- # +after_commit+ and +after_rollback+ callbacks are executed.
+ # +after_commit+ and +after_rollback+ callbacks are executed. No dirty-tracking
+ # occurs, meaning that the `Dirty` methods, like `changed?`, `previous_changes`,
+ # and `changes` return the same values before and after `touch`.
# |
I guess that makes some sense... But we still need some way to tell which attributes were changed by the Could we introduce another mutation tracker that gets reset before the Or, if that seems like overkill (though with the existing complexity surrounding |
👍 by @y-yagi in rails#33429 [ci skip]
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Not stale. Waiting for #34306 (improves docs) to be merged. |
Thanks! |
Dear contributors, Reading carefully this issue and the related Pull Requests, I am still a bit confused with the behavior of Dirty with It looks like touching objects "manually" versus "in cascade through belongs_to association" does not result in the same behavior. Given the following models: class Project < ActiveRecord::Base
has_many :documents
after_touch :my_after_touch_callback
def my_after_touch_callback
puts "updated_at_before_last_save:#{updated_at_before_last_save}"
end
end
class Document < ActiveRecord::Base
belongs_to :project, touch: true
end I can observe different behaviors with Dirty methods: Project.last.touch
# updated_at_before_last_save:2019-11-24 12:58:24 UTC
Project.last.documents.last.touch
# updated_at_before_last_save: Am I wrong somewhere with my investigation? Is that the expected behavior regarding this issue resolution? I was not able to find documentation about that, please apologize if my question is kind of newbie :-) |
Hi Jean-Michel.
Not sure if it's a bug, but I can reproduce this in rails latest (6.0.1) with a script However,
Perhaps
I think this is quite a complicated question, actually 😄 très difficile |
Thank you for your answer @jaredbeck :-) |
…as expected in Rails < 6 because of these bugs: - rails/rails#33429 - rails/rails#30466 This lead to that `saved_changes` always contained all attributes (for the test!) and therefore `saved_change_to_attribute?` always returned true in `app/models/concerns/has_ticket_create_screen_impact.rb`. This also means that `#touch` actually never triggered the queueing of the `TicketCreateScreenJob` before the update to Rails 6. That's because `saved_changes` was empty for `#touch` saves. With Rails 6 `saved_changes` contains `updated_at` for `#touch` saves. Therefore we have to add `updated_at` to our `saved_change_to_attribute?` check list. TODO: We need to check the impact of this.
touch
only updates timestamps. Therefore, during theafter_touch
callback, I'd expectsaved_changes
to only contain those timestamps, but ..The text was updated successfully, but these errors were encountered: