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
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"git_source(:github){ |repo| "https://github.com/#{repo}.git"}gem"rails",github: "rails/rails",branch: "main"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:books,force: true,primary_key: %i[number]do |t|
t.integer:numbert.string:titlet.integer:main_chapter_number,null: trueendcreate_table:chapters,force: true,primary_key: %i[book_numbernumber]do |t|
t.integer:book_numbert.integer:numbert.string:titleendadd_foreign_key:books,:chapters,column: [:number,:main_chapter_number],primary_key: [:book_number,:number]endclassBook < ActiveRecord::Baseself.primary_key=%i[number]has_many:chapters,foreign_key: :book_number,inverse_of: :bookbelongs_to:main_chapter,class_name: "Chapter",query_constraints: %i[numbermain_chapter_number],optional: true,inverse_of: :bookendclassChapter < ActiveRecord::Baseself.primary_key=%i[book_numbernumber]belongs_to:book,foreign_key: :book_number,inverse_of: :chaptersendclassBugTest < Minitest::Testdeftest_association_stuffbook=Book.create!(number: 1,title: "First book")book.chapters << Chapter.new(book:,number: 1,title: "Chapter one") << Chapter.new(book:,number: 2,title: "Chapter two")assert_equal2,book.chapters.countassert_equal2,Chapter.countassert_equalnil,book.main_chapter_numberbook.update!(main_chapter: book.chapters.first)assert_equal1,book.main_chapter_number# book.reload # This is a workaround that will make it work, but it should not be necessarybook.update!(main_chapter_number: nil)# This one will not update the database.assert_equalnil,book.main_chapter_numberendend
The text was updated successfully, but these errors were encountered:
jarl-dk
changed the title
#update on CPK association requires a explicit reload to work correctly
#update on CPK association requires an explicit reload to work correctly
Mar 1, 2024
@jarl-dk , This issue is similar to the one addressed by #50297. The root cause for this one seems to be related to the stale state detection. I tested your script against that branch and can confirm it fixes the issue. You can verify the same using the following.
@jarl-dk , Since the PR is not merged yet, I think instead of closing this issue we should keep it open or maybe mark as duplicate. I can go ahead and update the PR description to include that this issue will be fixed as well.
Steps to reproduce
Save the below script as
ar.rb
and runExpected behavior
I expect
Actual behavior
It fails with
See comment in script for workaround
System configuration
Rails version:
main
Ruby version: 3.2.2
Maybe related issues: #49597
The text was updated successfully, but these errors were encountered: