aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-02-19 00:20:53 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-02-19 00:20:53 +0900
commit1af039dd3dfd68f40dc37b9a75b0d8a3dc8b1efe (patch)
treebae7948300d2db07a22fa29e1e82fe80a541ed29 /activerecord
parentfb86ecd604a362355827bbf05776b847b0ded9a5 (diff)
downloadrails-1af039dd3dfd68f40dc37b9a75b0d8a3dc8b1efe.tar.gz
rails-1af039dd3dfd68f40dc37b9a75b0d8a3dc8b1efe.tar.bz2
rails-1af039dd3dfd68f40dc37b9a75b0d8a3dc8b1efe.zip
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]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/reflection.rb14
1 files changed, 4 insertions, 10 deletions
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?