diff options
author | Tu Hoang <rebyn@me.com> | 2014-10-15 20:25:09 +0700 |
---|---|---|
committer | Tu Hoang <rebyn@me.com> | 2014-10-15 20:25:09 +0700 |
commit | f260f9cd294f33301a4971e44a25cccdd95273d6 (patch) | |
tree | 8906f73f9220eb50d120d784ad5390fc7005ff09 /activerecord | |
parent | 62f96c9e1fdfa5b832073f90e1fe592fbf3163bb (diff) | |
download | rails-f260f9cd294f33301a4971e44a25cccdd95273d6.tar.gz rails-f260f9cd294f33301a4971e44a25cccdd95273d6.tar.bz2 rails-f260f9cd294f33301a4971e44a25cccdd95273d6.zip |
Raise an error for has_one associations which try to go :through a polymorphic association [#17263]
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 |