Do not delete users when deleting legislation answers

When a legislation process is deleted, everything related will be
deleted, including the answers. This `dependent: :destroy` was causing
that users accounts were being accidentally deleted.
This commit is contained in:
Julian Herrero
2020-07-27 16:47:52 +07:00
committed by Javi Martín
parent c03ada579d
commit 46c78fc3ef
2 changed files with 9 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ class Legislation::Answer < ApplicationRecord
belongs_to :question, foreign_key: "legislation_question_id", inverse_of: :answers, counter_cache: true
belongs_to :question_option, foreign_key: "legislation_question_option_id",
inverse_of: :answers, counter_cache: true
belongs_to :user, dependent: :destroy, inverse_of: :legislation_answers
belongs_to :user, inverse_of: :legislation_answers
validates :question, presence: true, uniqueness: { scope: :user_id }
validates :question_option, presence: true

View File

@@ -36,4 +36,12 @@ RSpec.describe Legislation::Answer, type: :model do
expect(option_2.answers_count).to eq 1
expect(option_1.answers_count).to eq 0
end
it "does not delete users that created the answer" do
user = legislation_answer.user
legislation_answer.destroy!
expect(user).not_to be_hidden
end
end