Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:42:32+00:00 2026-06-04T20:42:32+00:00

I have this custom validations that throws an undefined method `>’ for nil:NilClass when

  • 0

I have this custom validations that throws an “undefined method `>’ for nil:NilClass” when ever birthday is not set because birthday is nil.

validate :is_21_or_older 
def is_21_or_older
  if birthday > 21.years.ago.to_date
    errors.add(:birthday, "Must 21 Or Older")
  end
end

I already have validates_presence_of for birthday so is there a way to have is_21_or_older called only after validates_presence_of passes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:42:33+00:00Added an answer on June 4, 2026 at 8:42 pm

    Rails runs all validators independently, in order to give you an array of all the errors at once. This is done to avoid the all too common scenario:

    Please enter a password.

    pass

    The password you have entered is invalid: it does not contain a number.

    1234

    The password you have entered is invalid: it does not contain a letter.

    a1234

    The password you have entered is invalid: it is not at least six characters long.

    ab1234

    The password you have entered is invalid: you cannot use three or more consecutive characters in a sequence.

    piss off

    The password you have entered is invalid: it does not contain a number.

    There are two things you can do, that I know of. Either include everything under your custom validator, in which case everything is under your control, or use the :unless => Proc.new { |x| x.birthday.nil? } modifier to explicitly restrict your validator from running under the circumstances it would break. I’d definitely suggest the first approach; the second is hacky.

    def is_21_or_older
      if birthday.blank?
        errors.add(:birthday, "Must have birthday")
      elsif birthday > 21.years.ago.to_date
        errors.add(:birthday, "Must 21 Or Older")
      end
    end
    

    Maybe an even better approach is to keep the presence validator, just exit your custom validator when it sees the other validator’s condition is failed.

    def is_21_or_older
      return true if birthday.blank? # let the other validator handle it
    
      if birthday > 21.years.ago.to_date
        errors.add(:birthday, "Must 21 Or Older")
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this custom textbox that I am working on and I can use
So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
Is it possible to have nested set capabilities in this somewhat custom setup? Consider
This is my custom model binder. I have my breakpoint set at BindModel but
I have a custom class that uses boost mutexes and locks like this (only
I have a ListView which contains custom rows. This custom row has following UI
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
REVISED QUESTION : We have tracked this down to a custom add to cart
I have encountered this error when trying to delete a cell, using a custom
I have a custom jsp tag like this: <a:customtag> The body of the custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.