To suppress the ugly error stacktrace
and error raised due to before_add
callback, what you can do is rescue
the raised error.
For example:
Update your question.rb
as below:
class AnswersLimitExceeded < StandardError; end ## Add this
class Question < ActiveRecord::Base
has_many :answers, -> { order(position: :asc) }, before_add: :set_nested
private
def set_nested(answer)
if answers.size > 3
errors.add(:base, "Too many answers.")
raise AnswersLimitExceeded, "Unable to create answer." ## Change here
else
answer.question ||= self
end
end
After this update the create
action in your QuestionsController
def create
## ...
begin
if @question.save
## ...
else
## ..
end
rescue AnswersLimitExceeded
render action: 'new'
end
end
This way whenever AnswersLimitExceeded
is raised within your Question
model while creating the associated answers
then it is rescued at controller level thereby suppressing the specific error AnswersLimitExceeded
with message Too many answers
elegantly.
Also, if you notice I created a separate Error class i.e., AnswersLimitExceeded
so that I rescue ONLY
this particular exception in Controller and not all others.