diff options
author | Diego Carrion <dc.rec1@gmail.com> | 2015-06-13 12:51:32 -0300 |
---|---|---|
committer | Diego Carrion <dc.rec1@gmail.com> | 2015-06-22 12:38:54 -0300 |
commit | 828d0d7a7751a0c6a9faf0c96b89b0ea06a76094 (patch) | |
tree | 44fac740ef8548424c137a2d39f28939538996bb /activerecord/lib | |
parent | ebc4c607a7859c9c35246d7fe74a17208ef3a66e (diff) | |
download | rails-828d0d7a7751a0c6a9faf0c96b89b0ea06a76094.tar.gz rails-828d0d7a7751a0c6a9faf0c96b89b0ea06a76094.tar.bz2 rails-828d0d7a7751a0c6a9faf0c96b89b0ea06a76094.zip |
thrown ActiveRecord::AssociationTypeMismatch when assigning a wrong value for a namespaced association
fixes #20541
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/association.rb | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 930f678ae8..7c729676a7 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -211,9 +211,12 @@ module ActiveRecord # the kind of the class of the associated objects. Meant to be used as # a sanity check when you are about to assign an associated record. def raise_on_type_mismatch!(record) - unless record.is_a?(reflection.klass) || record.is_a?(reflection.class_name.constantize) - message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})" - raise ActiveRecord::AssociationTypeMismatch, message + unless record.is_a?(reflection.klass) + fresh_class = reflection.class_name.safe_constantize + unless fresh_class && record.is_a?(fresh_class) + message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})" + raise ActiveRecord::AssociationTypeMismatch, message + end end end |