forked from rails/rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing ordering of HABTM association deletion [rails#6191 state:resol…
…ved] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
- Loading branch information
1 parent
df07760
commit 909588d
Showing
6 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Student < ActiveRecord::Base | ||
has_and_belongs_to_many :lessons | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters