Permalink
Browse files

Make belongs_to :dependent => :destroy destroy self before associated…

… object [#1079 state:resolved]

If foreign key constraints are in place then deleteing the associated object first will cause a foreign key violation

Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
  • Loading branch information...
Ben VandenBos authored and lifo committed Jan 5, 2009
1 parent f2ee3f2 commit 0e92f67073079fe3d34acd141099cdad28b0ee00
@@ -1531,14 +1531,14 @@ def configure_dependency_for_belongs_to(reflection)
association = send(reflection.name)
association.destroy unless association.nil?
end
- before_destroy method_name
+ after_destroy method_name
when :delete
method_name = "belongs_to_dependent_delete_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.delete unless association.nil?
end
- before_destroy method_name
+ after_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})"
end
@@ -698,7 +698,8 @@ def test_dependent_delete_and_destroy_with_belongs_to
authors(:david).destroy
end
- assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
+ assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_id)
+ assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_extra_id)
end
def test_invalid_belongs_to_dependent_option_raises_exception

0 comments on commit 0e92f67

Please sign in to comment.