Skip to content

Commit

Permalink
Attempt to fix issue #402 #402
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas D'Stefano committed May 31, 2011
1 parent 56f13a3 commit 48d735f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1706,14 +1706,14 @@ def #{method_name}
end

def configure_after_destroy_method_for_has_and_belongs_to_many(reflection)
method_name = :"has_and_belongs_to_many_after_destroy_for_#{reflection.name}"
method_name = :"has_and_belongs_to_many_before_destroy_for_#{reflection.name}"
class_eval <<-eoruby, __FILE__, __LINE__ + 1
def #{method_name}
association = #{reflection.name}
association.delete_all if association
end
eoruby
after_destroy method_name
before_destroy method_name
end

def delete_all_has_many_dependencies(record, reflection_name, association_class, dependent_conditions)
Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/habtm_destroy_order_test.rb
Expand Up @@ -16,6 +16,19 @@ class HabtmDestroyOrderTest < ActiveRecord::TestCase
assert !sicp.destroyed?
end

test 'may not raise error if have foreign key in the join table' do
# TODO: Using foreign keys. Add conditionals if need to around dbs that support it.
Lesson.connection.execute "ALTER TABLE lessons_students ADD CONSTRAINT lesson_id_fk FOREIGN KEY (lesson_id) REFERENCES lessons(id)"
Student.connection.execute "ALTER TABLE lessons_students ADD CONSTRAINT student_id_fk FOREIGN KEY (student_id) REFERENCES students(id)"
student = Student.new(:name => "Ben Bitdiddle")
lesson = Lesson.new(:name => "SICP")
lesson.students << student
lesson.save!
assert_nothing_raised do
student.destroy
end
end

test "not destroying a student with lessons leaves student<=>lesson association intact" do
# test a normal before_destroy doesn't destroy the habtm joins
begin
Expand Down

0 comments on commit 48d735f

Please sign in to comment.