How do I validate that a question has at least one answer?
My question model h开发者_Python百科as_many :answers in Rails 3. I want to be able to validate that each question has at least one answer.
I tried the following code:
validate :must_have_answer
def must_have_answer
if answers.empty? or answers.all?{ |example| example.marked_for_destruction? }
errors.add_to_base{ "Must provide at least one answer"}
end
end
I got the following error:
wrong number of arguments (0 for 1)
Try passing a string instead of a block to errors.add_to_base
errors.add_to_base "Must provide at least one answer"
精彩评论