aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-02-26 05:15:22 +0900
committerGitHub <noreply@github.com>2018-02-26 05:15:22 +0900
commit91b30a001b79096b60d9424a4664a417dce0b767 (patch)
treeef0ce40e074e30e4674a4b4c0f776bbc9a444c36 /activerecord/lib/active_record
parent8ff70cad3d634da344d741c49304a69572cbe188 (diff)
parent1af039dd3dfd68f40dc37b9a75b0d8a3dc8b1efe (diff)
downloadrails-91b30a001b79096b60d9424a4664a417dce0b767.tar.gz
rails-91b30a001b79096b60d9424a4664a417dce0b767.tar.bz2
rails-91b30a001b79096b60d9424a4664a417dce0b767.zip
Merge pull request #31895 from kamipo/do_not_attempt_to_find_inverse_of_polymorphic
Make `reflection.klass` raise if `polymorphic?` not to be misused
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/reflection.rb17
1 files changed, 7 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index da75344bde..71afbc1041 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -416,6 +416,9 @@ module ActiveRecord
# Active Record class.
class AssociationReflection < MacroReflection #:nodoc:
def compute_class(name)
+ if polymorphic?
+ raise ArgumentError, "Polymorphic association does not support to compute class."
+ end
active_record.send(:compute_type, name)
end
@@ -608,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 NameError
- # 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
@@ -626,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 &&
@@ -732,6 +726,9 @@ module ActiveRecord
end
private
+ def can_find_inverse_of_automatically?(_)
+ !polymorphic? && super
+ end
def calculate_constructable(macro, options)
!polymorphic?