From 1af039dd3dfd68f40dc37b9a75b0d8a3dc8b1efe Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 19 Feb 2018 00:20:53 +0900 Subject: Do not attempt to automatically find the inverse of a polymorphic `belongs_to` association We can't automatically find the inverse of a polymorphic `belongs_to` association without context. [Ryuta Kamizono & Eric K Idema] --- activerecord/lib/active_record/reflection.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 101ac2d33a..8950be1ab7 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -611,13 +611,7 @@ module ActiveRecord if can_find_inverse_of_automatically?(self) inverse_name = ActiveSupport::Inflector.underscore(options[:as] || active_record.name.demodulize).to_sym - begin - reflection = klass._reflect_on_association(inverse_name) - rescue ArgumentError - # Give up: we couldn't compute the klass type so we won't be able - # to find any associations either. - reflection = false - end + reflection = klass._reflect_on_association(inverse_name) if valid_inverse_reflection?(reflection) return inverse_name @@ -629,9 +623,6 @@ module ActiveRecord # +automatic_inverse_of+ method is a valid reflection. We must # make sure that the reflection's active_record name matches up # with the current reflection's klass name. - # - # Note: klass will always be valid because when there's a NameError - # from calling +klass+, +reflection+ will already be set to false. def valid_inverse_reflection?(reflection) reflection && klass <= reflection.active_record && @@ -735,6 +726,9 @@ module ActiveRecord end private + def can_find_inverse_of_automatically?(_) + !polymorphic? && super + end def calculate_constructable(macro, options) !polymorphic? -- cgit v1.2.3