diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-02-05 21:40:44 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-02-19 00:11:29 +0900 |
commit | fb86ecd604a362355827bbf05776b847b0ded9a5 (patch) | |
tree | 8e0d28ede1e72023c636864e2874f64001a1ff94 /activerecord/lib/active_record | |
parent | 5ece2e4a4459065b5efd976aebd209bbf0cab89b (diff) | |
download | rails-fb86ecd604a362355827bbf05776b847b0ded9a5.tar.gz rails-fb86ecd604a362355827bbf05776b847b0ded9a5.tar.bz2 rails-fb86ecd604a362355827bbf05776b847b0ded9a5.zip |
Make `reflection.klass` raise if `polymorphic?` not to be misused
This is an alternative of #31877 to fix #31876 caused by #28808.
This issue was caused by a combination of several loose implementation.
* finding automatic inverse association of polymorphic without context (caused by #28808)
* returning `klass` even if `polymorphic?` (exists before #28808)
* loose verification by `valid_inverse_reflection?` (exists before #28808)
This makes `klass` raise if `polymorphic?` not to be misused.
This issue will not happen unless polymorphic `klass` is misused.
Fixes #31876.
Closes #31877.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index c66c7778f4..101ac2d33a 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 @@ -610,7 +613,7 @@ module ActiveRecord begin reflection = klass._reflect_on_association(inverse_name) - rescue NameError + rescue ArgumentError # Give up: we couldn't compute the klass type so we won't be able # to find any associations either. reflection = false |