本文介绍了ArgumentError:您需要提供至少一个验证:如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型

class Task < ActiveRecord::Base
  validates :deadline, :if => :deadline_in_future?

  def deadline_in_future?
    Date.today < self.deadline
  end
end

看起来一切正常,但是当我在 Rails 控制台中时

All seems ok, but when I in my rails console

irb(main):001:0> Task.new
ArgumentError: You need to supply at least one validation

问题出在哪里?

推荐答案

你忘了告诉 validates 你想如何验证 :deadline.我认为您误解了 :if 的作用;:if =>:deadline_in_future? 选项表示:

You forgot to tell validates how you want to validate :deadline. I think you're misunderstanding what :if does; the :if => :deadline_in_future? option means:

仅当 deadline_in_future? 方法返回真值时才验证 :deadline.

我怀疑您想验证截止日期是在未来:

I suspect that you want to validate that the deadline is in the future:

validate :deadline_in_future?

Active Record 验证和回调指南中提供了更多详细信息.

Further details are available in the Active Record Validations and Callbacks Guide.

这篇关于ArgumentError:您需要提供至少一个验证:如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:51