开发者

How do I validate that a question has at least one answer?

开发者 https://www.devze.com 2023-03-12 09:13 出处:网络
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.

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消