mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix misleading errors for has_one through relations
This commit is contained in:
parent
efd2410b39
commit
4f1ec3ac96
5 changed files with 36 additions and 8 deletions
|
@ -61,12 +61,18 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasManyThroughCantAssociateThroughHasOneOrManyReflection < ActiveRecordError #:nodoc:
|
class ThroughCantAssociateThroughHasOneOrManyReflection < ActiveRecordError #:nodoc:
|
||||||
def initialize(owner, reflection)
|
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}.")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class HasManyThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection #:nodoc:
|
||||||
|
end
|
||||||
|
|
||||||
|
class HasOneThroughCantAssociateThroughHasOneOrManyReflection < ThroughCantAssociateThroughHasOneOrManyReflection #:nodoc:
|
||||||
|
end
|
||||||
|
|
||||||
class HasManyThroughCantAssociateNewRecords < ActiveRecordError #:nodoc:
|
class HasManyThroughCantAssociateNewRecords < ActiveRecordError #:nodoc:
|
||||||
def initialize(owner, reflection)
|
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.")
|
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 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasManyThroughNestedAssociationsAreReadonly < ActiveRecordError #:nodoc:
|
class ThroughNestedAssociationsAreReadonly < ActiveRecordError #:nodoc:
|
||||||
def initialize(owner, reflection)
|
def initialize(owner, reflection)
|
||||||
super("Cannot modify association '#{owner.class.name}##{reflection.name}' because it goes through more than one other association.")
|
super("Cannot modify association '#{owner.class.name}##{reflection.name}' because it goes through more than one other association.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class HasManyThroughNestedAssociationsAreReadonly < ThroughNestedAssociationsAreReadonly #:nodoc:
|
||||||
|
end
|
||||||
|
|
||||||
|
class HasOneThroughNestedAssociationsAreReadonly < ThroughNestedAssociationsAreReadonly #:nodoc:
|
||||||
|
end
|
||||||
|
|
||||||
class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
|
class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
|
||||||
def initialize(reflection)
|
def initialize(reflection)
|
||||||
super("Cannot eagerly load the polymorphic association #{reflection.name.inspect}")
|
super("Cannot eagerly load the polymorphic association #{reflection.name.inspect}")
|
||||||
|
|
|
@ -76,13 +76,21 @@ module ActiveRecord
|
||||||
|
|
||||||
def ensure_mutable
|
def ensure_mutable
|
||||||
unless source_reflection.belongs_to?
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_not_nested
|
def ensure_not_nested
|
||||||
if reflection.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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -352,4 +352,11 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
assert_deprecated { member.club(true) }
|
assert_deprecated { member.club(true) }
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -495,7 +495,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
groucho = members(:groucho)
|
groucho = members(:groucho)
|
||||||
founding = member_types(:founding)
|
founding = member_types(:founding)
|
||||||
|
|
||||||
assert_raises(ActiveRecord::HasManyThroughNestedAssociationsAreReadonly) do
|
assert_raises(ActiveRecord::HasOneThroughNestedAssociationsAreReadonly) do
|
||||||
groucho.nested_member_type = founding
|
groucho.nested_member_type = founding
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
class MemberDetail < ActiveRecord::Base
|
class MemberDetail < ActiveRecord::Base
|
||||||
belongs_to :member, :inverse_of => false
|
belongs_to :member, inverse_of: false
|
||||||
belongs_to :organization
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue