Skip to content

Commit

Permalink
Fixing ordering of HABTM association deletion [rails#6191 state:resol…
Browse files Browse the repository at this point in the history
…ved]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
ef4 authored and spastorino committed Feb 4, 2011
1 parent df07760 commit 909588d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -1450,8 +1450,8 @@ def has_and_belongs_to_many(association_id, options = {}, &extension)
include Module.new {
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def destroy # def destroy
#{reflection.name}.clear # posts.clear
super # super
#{reflection.name}.clear # posts.clear
end # end
RUBY
}
Expand Down
Expand Up @@ -365,7 +365,7 @@ def test_deleting_all
def test_removing_associations_on_destroy
david = DeveloperWithBeforeDestroyRaise.find(1)
assert !david.projects.empty?
assert_raise(RuntimeError) { david.destroy }
david.destroy
assert david.projects.empty?
assert DeveloperWithBeforeDestroyRaise.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = 1").empty?
end
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/habtm_destroy_order_test.rb
@@ -0,0 +1,17 @@
require "cases/helper"
require "models/lesson"
require "models/student"

class HabtmDestroyOrderTest < ActiveRecord::TestCase
test "may not delete a lesson with students" do
sicp = Lesson.new(:name => "SICP")
ben = Student.new(:name => "Ben Bitdiddle")
sicp.students << ben
sicp.save!
assert_raises LessonError do
assert_no_difference('Lesson.count') do
sicp.destroy
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/test/models/lesson.rb
@@ -0,0 +1,11 @@
class LessonError < Exception
end

class Lesson < ActiveRecord::Base
has_and_belongs_to_many :students
before_destroy :ensure_no_students

def ensure_no_students
raise LessonError unless students.empty?
end
end
3 changes: 3 additions & 0 deletions activerecord/test/models/student.rb
@@ -0,0 +1,3 @@
class Student < ActiveRecord::Base
has_and_belongs_to_many :lessons
end
13 changes: 13 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -279,6 +279,15 @@ def create_table(*args, &block)
t.integer :version, :null => false, :default => 0
end

create_table :lessons, :force => true do |t|
t.string :name
end

create_table :lessons_students, :id => false, :force => true do |t|
t.references :lesson
t.references :student
end

create_table :line_items, :force => true do |t|
t.integer :invoice_id
t.integer :amount
Expand Down Expand Up @@ -509,6 +518,10 @@ def create_table(*args, &block)
t.string :sponsorable_type
end

create_table :students, :force => true do |t|
t.string :name
end

create_table :subscribers, :force => true, :id => false do |t|
t.string :nick, :null => false
t.string :name
Expand Down

0 comments on commit 909588d

Please sign in to comment.