Skip to content

Commit

Permalink
Merge pull request #40540 from sekiyama58/has_many_find_multi_id_inve…
Browse files Browse the repository at this point in the history
…rse_of

Set the inverse record on find with multiple ids from has_many association
  • Loading branch information
kamipo committed Nov 6, 2020
2 parents 53e97f0 + 996b535 commit 20ce878
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/relation/spawn_methods.rb
Expand Up @@ -69,8 +69,8 @@ def only(*onlies)

private
def relation_with(values)
result = Relation.create(klass, values: values)
result.extend(*extending_values) if extending_values.any?
result = spawn
result.instance_variable_set(:@values, values)
result
end
end
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/associations/inverse_associations_test.rb
Expand Up @@ -509,6 +509,24 @@ def test_find_on_child_instance_with_id_should_not_load_all_child_records
assert_not_predicate human.interests, :loaded?
end

def test_find_on_child_instance_with_id_should_set_inverse_instances
human = Human.create!
interest = Interest.create!(human: human)

child = human.interests.find(interest.id)
assert_predicate child.association(:human), :loaded?
end

def test_find_on_child_instances_with_ids_should_set_inverse_instances
human = Human.create!
interests = Array.new(2) { Interest.create!(human: human) }

children = human.interests.find(interests.pluck(:id))
children.each do |child|
assert_predicate child.association(:human), :loaded?
end
end

def test_raise_record_not_found_error_when_invalid_ids_are_passed
# delete all interest records to ensure that hard coded invalid_id(s)
# are indeed invalid.
Expand Down

0 comments on commit 20ce878

Please sign in to comment.