Skip to content

Commit 9232363

Browse files
authored
Merge pull request rails#51929 from Skalar/source-reflection-regression
Make source_reflection return nil when no name
2 parents ae3c93d + 2364b42 commit 9232363

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

activerecord/lib/active_record/reflection.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,8 @@ def klass
10071007
# # => <ActiveRecord::Reflection::BelongsToReflection: @name=:tag, @active_record=Tagging, @plural_name="tags">
10081008
#
10091009
def source_reflection
1010+
return unless source_reflection_name
1011+
10101012
through_reflection.klass._reflect_on_association(source_reflection_name)
10111013
end
10121014

activerecord/test/cases/reflection_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,18 @@ def test_reflect_on_association_accepts_strings
607607
end
608608
end
609609

610+
def test_reflect_on_missing_source_assocation
611+
assert_nothing_raised do
612+
assert_nil Hotel.reflect_on_association(:lost_items).source_reflection
613+
end
614+
end
615+
616+
def test_reflect_on_missing_source_assocation_raise_exception
617+
assert_raises(ActiveRecord::HasManyThroughSourceAssociationNotFoundError) do
618+
Hotel.reflect_on_association(:lost_items).check_validity!
619+
end
620+
end
621+
610622
def test_name_error_from_incidental_code_is_not_converted_to_name_error_for_association
611623
UserWithInvalidRelation.stub(:const_missing, proc { oops }) do
612624
reflection = UserWithInvalidRelation.reflect_on_association(:not_a_class)

activerecord/test/models/hotel.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ class Hotel < ActiveRecord::Base
1010
has_many :mocktail_designers, through: :chef_lists, source: :employable, source_type: "MocktailDesigner"
1111

1212
has_many :recipes, through: :chefs
13+
14+
has_many :lost_items, through: :departments
1315
end

0 commit comments

Comments
 (0)