mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Raise an error for associations which try to go :through a polymorphic association [#6212 state:resolved]
This commit is contained in:
parent
1c07b84df9
commit
fb3a8c51b4
4 changed files with 22 additions and 6 deletions
|
@ -20,12 +20,18 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasManyThroughAssociationPolymorphicError < ActiveRecordError #:nodoc:
|
class HasManyThroughAssociationPolymorphicSourceError < ActiveRecordError #:nodoc:
|
||||||
def initialize(owner_class_name, reflection, source_reflection)
|
def initialize(owner_class_name, reflection, source_reflection)
|
||||||
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}'.")
|
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' on the polymorphic object '#{source_reflection.class_name}##{source_reflection.name}'.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class HasManyThroughAssociationPolymorphicThroughError < ActiveRecordError #:nodoc:
|
||||||
|
def initialize(owner_class_name, reflection)
|
||||||
|
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' which goes through the polymorphic association '#{owner_class_name}##{reflection.through_reflection.name}'.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class HasManyThroughAssociationPointlessSourceTypeError < ActiveRecordError #:nodoc:
|
class HasManyThroughAssociationPointlessSourceTypeError < ActiveRecordError #:nodoc:
|
||||||
def initialize(owner_class_name, reflection, source_reflection)
|
def initialize(owner_class_name, reflection, source_reflection)
|
||||||
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' with a :source_type option if the '#{reflection.through_reflection.class_name}##{source_reflection.name}' is not polymorphic. Try removing :source_type on your association.")
|
super("Cannot have a has_many :through association '#{owner_class_name}##{reflection.name}' with a :source_type option if the '#{reflection.through_reflection.class_name}##{source_reflection.name}' is not polymorphic. Try removing :source_type on your association.")
|
||||||
|
|
|
@ -375,6 +375,10 @@ module ActiveRecord
|
||||||
raise HasManyThroughAssociationNotFoundError.new(active_record.name, self)
|
raise HasManyThroughAssociationNotFoundError.new(active_record.name, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if through_reflection.options[:polymorphic]
|
||||||
|
raise HasManyThroughAssociationPolymorphicThroughError.new(active_record.name, self)
|
||||||
|
end
|
||||||
|
|
||||||
if source_reflection.nil?
|
if source_reflection.nil?
|
||||||
raise HasManyThroughSourceAssociationNotFoundError.new(self)
|
raise HasManyThroughSourceAssociationNotFoundError.new(self)
|
||||||
end
|
end
|
||||||
|
@ -384,7 +388,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
if source_reflection.options[:polymorphic] && options[:source_type].nil?
|
if source_reflection.options[:polymorphic] && options[:source_type].nil?
|
||||||
raise HasManyThroughAssociationPolymorphicError.new(active_record.name, self, source_reflection)
|
raise HasManyThroughAssociationPolymorphicSourceError.new(active_record.name, self, source_reflection)
|
||||||
end
|
end
|
||||||
|
|
||||||
unless [:belongs_to, :has_many, :has_one].include?(source_reflection.macro) && source_reflection.options[:through].nil?
|
unless [:belongs_to, :has_many, :has_one].include?(source_reflection.macro) && source_reflection.options[:through].nil?
|
||||||
|
|
|
@ -340,11 +340,16 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_has_many_polymorphic
|
def test_has_many_polymorphic
|
||||||
assert_raise ActiveRecord::HasManyThroughAssociationPolymorphicError do
|
assert_raise ActiveRecord::HasManyThroughAssociationPolymorphicSourceError do
|
||||||
assert_equal posts(:welcome, :thinking), tags(:general).taggables
|
tags(:general).taggables
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assert_raise ActiveRecord::HasManyThroughAssociationPolymorphicThroughError do
|
||||||
|
taggings(:welcome_general).things
|
||||||
|
end
|
||||||
|
|
||||||
assert_raise ActiveRecord::EagerLoadPolymorphicError do
|
assert_raise ActiveRecord::EagerLoadPolymorphicError do
|
||||||
assert_equal posts(:welcome, :thinking), tags(:general).taggings.find(:all, :include => :taggable, :conditions => 'bogus_table.column = 1')
|
tags(:general).taggings.find(:all, :include => :taggable, :conditions => 'bogus_table.column = 1')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,4 +7,5 @@ class Tagging < ActiveRecord::Base
|
||||||
belongs_to :super_tag, :class_name => 'Tag', :foreign_key => 'super_tag_id'
|
belongs_to :super_tag, :class_name => 'Tag', :foreign_key => 'super_tag_id'
|
||||||
belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id'
|
belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id'
|
||||||
belongs_to :taggable, :polymorphic => true, :counter_cache => true
|
belongs_to :taggable, :polymorphic => true, :counter_cache => true
|
||||||
end
|
has_many :things, :through => :taggable
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue