Validation gotcha (before_save not being called)

October 18th, 2010 by e-thang Leave a reply »

So once I figured it out, there was a generous *facepalm*. Nonetheless, in case you’re struggling with the same problem, here it goes.

I had a before_validation callback on an ActiveRecord model. I was running in test mode using Shoulda and FactoryGirl, so I figured those 2 gems had something to do with it. Then came the humble pie.

I also had a validates_presence_of on the model, and it was getting called before the before_validation callback. The test wasn’t setting that attribute, so naturally I never got to the callback.

Once again it was not a cloud if improbability that caused the universe to collapse around me. It was a bone-headed error.



  1. Tyler Collier says:

    Thanks for this tip! I was using before_save, and should have been using before_validation.

  2. Carlos Santana says:

    Same here! Thanks, it was taking me forever to understand why this wasn’t working. So glad people like you take the time to write posts like this.

  3. e-thang says:

    Awesome! Glad it could help.

Leave a Reply