You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
beginrequire"bundler/inline"rescueLoadError=>e
$stderr.puts"Bundler version 1.10 or later is required. Please update your Bundler"raiseeendgemfile(true)dosource"https://rubygems.org"gem"rails",github: 'rails/rails'gem"sqlite3"endrequire"active_record"require"minitest/autorun"require"logger"# This connection will do for database-independent bug reports.ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:usersdo |t|
t.references:representer,null: falset.timestampsendcreate_table:representersdo |t|
t.timestampsendcreate_table:postsdo |t|
t.references:representer,null: falset.timestampsendcreate_table:logsdo |t|
t.timestampsendendclassLog < ActiveRecord::Base;endclassUser < ActiveRecord::Basebelongs_to:representerafter_commit->{Log.create!}endclassRepresenter < ActiveRecord::Basehas_one:userafter_commit->{Log.create!}endclassPost < ActiveRecord::Basebelongs_to:representerhas_one:user,through: :representerendclassBugTest < Minitest::Testdefsetup# It's false positive when post_id == user_id# make user_id different from post_idUser.create!(representer: Representer.create!)@representer=Representer.create!@user=User.create!(representer: @representer)@post=Post.create!(representer: @representer)enddeftest_autosave_for_a_userlog_count=Log.count@post.tap(&:user).save!assert_equal(log_count,Log.count)enddeftest_autosave_for_a_representerlog_count=Log.count@post.tap(&:representer).save!assert_equal(log_count,Log.count)endend
Expected behavior
after_commit should not be called on User on Post saving
Actual behavior
after_commit is called on User despite it was not changed
Because User has foreign_key for the representer, and it's representer_id, not post_id. When both IDs don't match, it thinks that the association was changed, and triggers commit for User too
The text was updated successfully, but these errors were encountered:
Kukunin
changed the title
Autosave happens every time on has_one :throught
Autosave happens every time on has_one :through
Mar 20, 2019
Fixesrails#35680
The problem occurred, when a `has one through` association contains
a foreign key (it belongs to the intermediate association).
For example, Comment belongs to Post, Post belongs to Author, and Author
`has_one :first_comment, through: :first_post`.
In this case, the value of the foreign key is comparing with the original
record, and since they are likely different, the association is marked
as changed. So it updates every time when the origin record updates.
Steps to reproduce
Expected behavior
after_commit
should not be called onUser
onPost
savingActual behavior
after_commit
is called onUser
despite it was not changedSystem configuration
Rails version:
git master 7c63430
Ruby version:
2.5.3p105
The problem happens here:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb#L460
Because User has foreign_key for the representer, and it's representer_id, not post_id. When both IDs don't match, it thinks that the association was changed, and triggers commit for User too
The text was updated successfully, but these errors were encountered: