Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 559
Ximik Avatar asked Nov 26 '11 22:11

Ximik


3 Answers

You must change validates to validate.

like image 82
zarne Avatar answered Nov 06 '22 20:11

zarne


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.

like image 26
mu is too short Avatar answered Nov 06 '22 20:11

mu is too short


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

like image 22
KL-7 Avatar answered Nov 06 '22 20:11

KL-7