Skip to content
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

Fix stale state for composite foreign keys in belongs_to association #50297

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,11 @@ def invertible_for?(record)
end

def stale_state
result = owner._read_attribute(reflection.foreign_key) { |n| owner.send(:missing_attribute, n, caller) }
result && result.to_s
result = Array(reflection.foreign_key).map do |fk|
owner._read_attribute(fk) { |n| owner.send(:missing_attribute, n, caller) }
end.join(",")
iamradioactive marked this conversation as resolved.
Show resolved Hide resolved

result.empty? ? nil : result
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
require "models/node"
require "models/club"
require "models/cpk"
require "models/sharded/blog"
require "models/sharded/blog_post"
require "models/sharded/comment"

class BelongsToAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :topics,
Expand Down Expand Up @@ -392,6 +395,18 @@ def test_should_set_composite_foreign_key_on_association_when_key_changes_on_ass
assert_equal 3, book.shop_id
end

def test_should_reload_association_on_model_with_query_constraints_when_foreign_key_changes
blog = Sharded::Blog.create!
blog_post = Sharded::BlogPost.create!(blog: blog)
comment = Sharded::Comment.create!(blog: blog)

# Load the association once
comment.blog_post
comment.blog_post_id = blog_post.id

assert_equal blog_post, comment.blog_post
end

def test_building_the_belonging_object_with_implicit_sti_base_class
account = Account.new
company = account.build_firm
Expand Down