ArgumentError: You need to supply at least one validation with :if

不问归期 提交于 2019-12-09 14:00:33

问题


I have a simple model

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

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

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

Where is the problem?


回答1:


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:

Validate :deadline only if the deadline_in_future? method returns a true value.

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

validate :deadline_in_future?

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




回答2:


You must change validates to validate.




回答3:


It says you do not pass any validations to validates method. Like validates :presence, for example. What are you trying to validate?



来源:https://stackoverflow.com/questions/8282167/argumenterror-you-need-to-supply-at-least-one-validation-with-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!