Skip to content

Instantly share code, notes, and snippets.

@kbuckler
Created October 13, 2009 02:04
Show Gist options
  • Save kbuckler/208910 to your computer and use it in GitHub Desktop.
Save kbuckler/208910 to your computer and use it in GitHub Desktop.
diff --git a/app/models/question.rb b/app/models/question.rb
index c38bbbb..7f6cfdc 100644
--- a/app/models/question.rb
+++ b/app/models/question.rb
@@ -10,6 +10,5 @@
class Question < ActiveRecord::Base
validates_presence_of :body
- has_many :answers # TODO dependent => :destroy?
-
+ has_many :answers, :dependent => :destroy
end
diff --git a/app/views/questions/index.html.erb b/app/views/questions/index.html.erb
index 54377e8..a11532b 100644
--- a/app/views/questions/index.html.erb
+++ b/app/views/questions/index.html.erb
@@ -6,7 +6,7 @@
<td style="width: 90%;"><%= h truncate(question.body, :length => 100) %></td>
<td><%= link_to 'Show', question %></td>
<td><%= link_to 'Edit', edit_question_path(question) %></td>
- <td><%= link_to 'Destroy', question, :confirm => 'Are you sure?', :method => :delete %></td>
+ <td><%= link_to 'Destroy', question, :confirm => 'Any answers to this question will be deleted too. Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
diff --git a/test/unit/question_test.rb b/test/unit/question_test.rb
index 4da9f4c..cd37159 100644
--- a/test/unit/question_test.rb
+++ b/test/unit/question_test.rb
@@ -21,4 +21,12 @@ class QuestionTest < ActiveSupport::TestCase
test "fails on blank input" do
assert_equal false, Question.new.save
end
+
+ test "destroys associated answers" do
+ question = questions(:one)
+ assert_equal question.answers.count, 2
+ assert_difference 'Answer.count', -2 do
+ question.destroy
+ end
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment