mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Raise an error for has_one associations which try to go :through a polymorphic association [#17263]
This commit is contained in:
parent
62f96c9e1f
commit
f260f9cd29
4 changed files with 16 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue