Merge pull request #20849 from vngrs/misleading_nested_exceptions

Fix misleading errors for has_one through relations
This commit is contained in:
Rafael Mendonça França 2015-07-27 17:28:24 -03:00
commit 722b2729b0
5 changed files with 36 additions and 8 deletions

@ -61,12 +61,18 @@ def initialize(reflection)
end
end
class HasManyThroughCantAssociateThroughHasOneOrManyReflection < ActiveRecordError #:nodoc:
class ThroughCantAssociateThroughHasOneOrManyReflection < ActiveRecordError #:nodoc:
def initialize(owner, reflection)
super("Cannot modify association '#{owner.class.name}##{reflection.name}' because the source reflection class '#{reflection.source_reflection.class_name}' is associated to '#{reflection.through_reflection.class_name}' via :#{reflection.source_reflection.macro}.")
end
end
class HasManyThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection #:nodoc:
end
class HasOneThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection #:nodoc:
end
class HasManyThroughCantAssociateNewRecords < ActiveRecordError #:nodoc:
def initialize(owner, reflection)
super("Cannot associate new records through '#{owner.class.name}##{reflection.name}' on '#{reflection.source_reflection.class_name rescue nil}##{reflection.source_reflection.name rescue nil}'. Both records must have an id in order to create the has_many :through record associating them.")
@ -79,12 +85,18 @@ def initialize(owner, reflection)
end
end
class HasManyThroughNestedAssociationsAreReadonly < ActiveRecordError #:nodoc:
class ThroughNestedAssociationsAreReadonly < ActiveRecordError #:nodoc:
def initialize(owner, reflection)
super("Cannot modify association '#{owner.class.name}##{reflection.name}' because it goes through more than one other association.")
end
end
class HasManyThroughNestedAssociationsAreReadonly < ThroughNestedAssociationsAreReadonly #:nodoc:
end
class HasOneThroughNestedAssociationsAreReadonly < ThroughNestedAssociationsAreReadonly #:nodoc:
end
class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
def initialize(reflection)
super("Cannot eagerly load the polymorphic association #{reflection.name.inspect}")

@ -76,13 +76,21 @@ def foreign_key_present?
def ensure_mutable
unless source_reflection.belongs_to?
raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
if reflection.has_one?
raise HasOneThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
else
raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
end
end
end
def ensure_not_nested
if reflection.nested?
raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection)
if reflection.has_one?
raise HasOneThroughNestedAssociationsAreReadonly.new(owner, reflection)
else
raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection)
end
end
end

@ -352,4 +352,11 @@ def test_association_force_reload_with_only_true_is_deprecated
assert_deprecated { member.club(true) }
end
def test_has_one_through_associations_are_mutable_unless_through_belongs_to
member_detail = MemberDetail.new(member: @member)
assert_raise(ActiveRecord::HasOneThroughCantAssociateThroughHasOneOrManyReflection) do
member_detail.membership = Membership.new
end
end
end

@ -495,7 +495,7 @@ def test_nested_has_one_through_writers_should_raise_error
groucho = members(:groucho)
founding = member_types(:founding)
assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
assert_raises(ActiveRecord::HasOneThroughNestedAssociationsAreReadonly) do
groucho.nested_member_type = founding
end
end

@ -1,7 +1,8 @@
class MemberDetail < ActiveRecord::Base
belongs_to :member, :inverse_of => false
belongs_to :member, inverse_of: false
belongs_to :organization
has_one :member_type, :through => :member
has_one :member_type, through: :member
has_one :membership, through: :member
has_many :organization_member_details, :through => :organization, :source => :member_details
has_many :organization_member_details, through: :organization, source: :member_details
end