diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-10-15 18:44:11 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-10-15 18:44:11 -0300 |
commit | 1130b5d38904a19966508154023fca5df7eecaab (patch) | |
tree | e26dee6ecebf7f820682a1fefba8cfc447483e75 /activerecord | |
parent | ba769cc4836cd909fd6dd659220790d9b98e4072 (diff) | |
parent | f260f9cd294f33301a4971e44a25cccdd95273d6 (diff) | |
download | rails-1130b5d38904a19966508154023fca5df7eecaab.tar.gz rails-1130b5d38904a19966508154023fca5df7eecaab.tar.bz2 rails-1130b5d38904a19966508154023fca5df7eecaab.zip |
Merge pull request #17267 from rebyn/master
Raise an error for has_one associations which try to go :through a polymorphic association
Diffstat (limited to 'activerecord')
4 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 12ca3a48a9..8911506694 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -46,6 +46,12 @@ module ActiveRecord end end + class HasOneAssociationPolymorphicThroughError < ActiveRecordError #:nodoc: + def initialize(owner_class_name, reflection) + super("Cannot have a has_one :through association '#{owner_class_name}##{reflection.name}' which goes through the polymorphic association '#{owner_class_name}##{reflection.through_reflection.name}'.") + end + end + class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError #:nodoc: def initialize(reflection) through_reflection = reflection.through_reflection diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 6b5a592ee5..cff70511f5 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -821,6 +821,7 @@ module ActiveRecord end if through_reflection.polymorphic? + raise HasOneAssociationPolymorphicThroughError.new(active_record.name, self) if has_one? raise HasManyThroughAssociationPolymorphicThroughError.new(active_record.name, self) end diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 089cb0a3a2..19d1aa87a8 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -289,6 +289,12 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase end end + def test_has_one_through_polymorphic_association + assert_raise(ActiveRecord::HasOneAssociationPolymorphicThroughError) do + @member.premium_club + end + end + def test_has_one_through_belongs_to_should_update_when_the_through_foreign_key_changes minivan = minivans(:cool_first) diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 72095f9236..dc0566d8a7 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -27,6 +27,9 @@ class Member < ActiveRecord::Base has_many :clubs, :through => :current_memberships has_one :club_through_many, :through => :current_memberships, :source => :club + + belongs_to :admittable, polymorphic: true + has_one :premium_club, through: :admittable end class SelfMember < ActiveRecord::Base |