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 6617575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:43:14+00:00 2026-05-25T20:43:14+00:00

I have several before_validation callbacks that operate on the attributes being set on my

  • 0

I have several before_validation callbacks that operate on the attributes being set on my model. I run into trouble when I have a situation like this:

class Foo < ActiveRecord::Base

  before_validation :capitalize_title
  validates :title, :presence => true

  def capitalize_title
    title.upcase
  end

end

I write a test to ensure that ‘nil’ title is not allowed, but the test gets an error because the nil.upcase is not defined. I’d like to handle this error, but I already have error handling that runs after before_validation callbacks.

I don’t want to put checks around all of my before_validation callbacks to make sure the data exists, if I can avoid it.

Is there a clean or accepted way to deal with this type of situation?

  • 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-05-25T20:43:15+00:00Added an answer on May 25, 2026 at 8:43 pm

    Just check if you have a title. And don’t forget to save the modified title.

    def capitalize_title
      title = title.upcase if title
    end
    

    If you need to patch things up with a before_validation hook then you’re stuck with taking care of invalid data in two places. If your validation was complicated you could factor it into two pieces: one piece that has to be true before the before_validation can run and one piece that has to be true after the before_validation has run:

    before_validation :mangle_data
    validate :data_is_okay
    
    #...
    
    def mangle_data
        return if(!data_is_mangleable)
        #... mangle away
    end
    
    def date_is_okay
        if(!data_is_mangleable)
            # complain
        end
        if(!data_is_properly_mangled)
            # complain some more
        end
    end
    
    def data_is_mangleable
       return false if(thing.nil?)
       # etc.
    end
    
    def data_is_properly_mangled
       # check that stuff that the before_validation hook doesn't
       # have to care about.
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have several jobs that run concurrently that have to use the same config
I have several old 3.5in floppy disks that I would like to backup. My
I have several related CVS projects, and I'd like to move them into subdirectories
I have several different c# worker applications that run various continuous tasks: sending emails
we have model helper (used by several different models) called set_guids that sets self.theguid
We have several apps that run on a Windows 2003/2008 server. Some of these
I have a set of pretty complex forms that I'd like to implement in
I have several HTML blocks on a page set up like: <p class=something> <a
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
I have several applications that are part of a suite of tools that various

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.