41

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?

KL-7
  • 46,000
  • 9
  • 87
  • 74
Ximik
  • 2,435
  • 3
  • 27
  • 53

3 Answers3

128

You must change validates to validate.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
zarne
  • 1,289
  • 2
  • 8
  • 2
  • 3
    @yeyo Because it's a syntax for custom validation. See [Custom Methods in Active Record Validations Doc](http://guides.rubyonrails.org/active_record_validations.html#custom-methods) – Radix Jan 13 '16 at 07:42
49

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
2

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

KL-7
  • 46,000
  • 9
  • 87
  • 74
  • I'm trying to do validation like this one http://guides.rubyonrails.org/active_record_validations_callbacks.html#using-a-symbol-with-if-and-unless – Ximik Nov 26 '11 at 22:54
  • 2
    In that example `:presence => true` says that presence of `card_number` field should be validated but validation should be performed only if `paid_with_card?` method returns true. The idea behind that is that order should have credit number if payment was made via credit card, but if payment was made not with credit card there's no need to check that card number is properly stored. – KL-7 Nov 26 '11 at 23:00