diff --git a/app/models/answer.rb b/app/models/answer.rb index e30747db7f7..49f993bc149 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -3,7 +3,7 @@ class Answer < ActiveRecord::Base attr_accessible :uid, :nid, :content, :cached_likes, :created_at, :updated_at - belongs_to :node, foreign_key: 'nid', dependent: :destroy + belongs_to :node, foreign_key: 'nid' belongs_to :drupal_user, foreign_key: 'uid' has_many :answer_selections, foreign_key: 'aid' has_many :comments, foreign_key: 'aid', dependent: :destroy diff --git a/app/models/node.rb b/app/models/node.rb index 9f267433f0f..2d3bbdfff91 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -49,7 +49,7 @@ def updated_month has_many :drupal_content_field_map_editor, foreign_key: 'nid' #, dependent: :destroy # re-enable in Rails 5 has_many :images, foreign_key: :nid has_many :node_selections, foreign_key: :nid - has_many :answers, foreign_key: :nid + has_many :answers, foreign_key: :nid, dependent: :destroy belongs_to :drupal_user, foreign_key: 'uid' diff --git a/test/functional/answers_controller_test.rb b/test/functional/answers_controller_test.rb index 7dad7ffb5d0..867a4f4029d 100644 --- a/test/functional/answers_controller_test.rb +++ b/test/functional/answers_controller_test.rb @@ -139,4 +139,17 @@ def setup assert answer.accepted end + test 'answer deletion should not delete the question node' do + UserSession.create(users(:moderator)) + answer = answers(:one) + answer.save + a = Answer.count + b = Node.count + + xhr :post, :delete, id: answer.id + + assert_equal Answer.count ,a - 1 + assert_equal Node.count,b + assert_response :success + end end diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index c5385489459..359449e1f3d 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -648,4 +648,20 @@ def test_get_rss_feed assert_equal "Only author can access the draft note", flash[:notice] end + test 'question deletion should delete all its answers' do + UserSession.create(users(:moderator)) + node = nodes(:question) + node.save + answer1 = answers(:one) + answer1.save + answer2 = answers(:two) + answer2.save + n_count = Node.count + + xhr :post, :delete, id: node.id + + assert_response :success + assert_equal Node.count, n_count - 1 + assert_equal Answer.count, 0 + end end