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

Only replace association keys if they've changed [5.2 Regression] #32375

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions activerecord/lib/active_record/associations/association.rb
Expand Up @@ -76,6 +76,11 @@ def target=(target)
loaded!
end

def preloaded_target=(target) #:nodoc:
@target = target
loaded!
end

def scope
target_scope.merge!(association_scope)
end
Expand Down
Expand Up @@ -46,7 +46,7 @@ def associate_records_to_owner(owner, records)
if reflection.collection?
association.target.concat(records)
else
association.target = records.first unless records.empty?
association.preloaded_target = records.first unless records.empty?
end
end

Expand Down
Expand Up @@ -1213,6 +1213,23 @@ def test_multiple_counter_cache_with_after_create_update
end
end
end

test "associations on destroyed models can be preloaded" do
post = posts(:welcome)
SelfDestructingComment.create!(post_id: post.id, body: "body")

comment = SelfDestructingComment.preload(:post).first
assert_equal post, comment.post
end
end

class SelfDestructingComment < ActiveRecord::Base
self.table_name = :comments
self.inheritance_column = nil

belongs_to :post

after_find { destroy! }
end

class BelongsToWithForeignKeyTest < ActiveRecord::TestCase
Expand Down