如何验证问题至少有一个答案?

时间:2011-06-10 01:13:22

标签: ruby-on-rails-3 validation

我的问题模型has_many:Rails中的答案3.我希望能够验证每个问题至少有一个答案。

我尝试了以下代码:

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

我收到以下错误:

wrong number of arguments (0 for 1)

1 个答案:

答案 0 :(得分:2)

尝试将字符串而不是块传递给errors.add_to_base

errors.add_to_base "Must provide at least one answer"