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

  • Home
  • SEARCH
  • 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 7724177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:37:35+00:00 2026-06-01T04:37:35+00:00

I want a validation to run before a record gets updated. I know of

  • 0

I want a validation to run before a record gets updated. I know of before_update but I pretty much copy and pasted the first codesnippet out of the api docs.

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

My stripped down model looked then like

class User < ActiveRecord::Base
  attr_accessible :email
  validates :email, :presence => true

  before_save(:on => :update) do
    puts "******** before_save on => :update ********"
    # do something
  end
end

if I go into the console and do create a new entry this callback is being executed on a SQL insert call.

irb(main):001:0> User.new(:email => "test@test.com").save
  (0.1ms)  begin transaction
******** before_save on => :update ********
SQL (29.1ms)  INSERT INTO "users" ("created_at", "email", "first_name", "last_name", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Fri, 30 Mar 2012 00:26:33 UTC +00:00], ["email", "test@test.com"], ["first_name", nil], ["last_name", nil], ["updated_at", Fri, 30 Mar 2012 00:26:33 UTC +00:00]]
   (433.1ms)  commit transaction
=> true
irb(main):002:0> 

I would have expected to see this only on an update call. Can anybody sheed some light on this?

[EDIT]

I just changed the callback into a function call with no change in the outcome. The callback is still executed on create.

class User < ActiveRecord::Base

attr_accessible :email
validates :email, :presence => true

before_save :my_before_update, :on => :update

private

def my_before_update
    puts "******** before_save on => :update ********"
    # do something
end

end

The output is the same.

Loading development environment (Rails 3.2.2)
irb(main):001:0> User.new(:email => "test@test.com").save
   (0.1ms)  begin transaction
******** before_save on => :update ********
  SQL (28.2ms)  INSERT INTO "users" ("created_at", "email", "first_name", "last_name",         "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Fri, 30 Mar 2012 02:28:45 UTC +00:00],     ["email", "test@test.com"], ["first_name", nil], ["last_name", nil], ["updated_at", Fri, 30     Mar 2012 02:28:45 UTC +00:00]]
   (131.2ms)  commit transaction
=> true
  • 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-01T04:37:36+00:00Added an answer on June 1, 2026 at 4:37 am

    The ActiveRecord::Callbacks don’t support an :on option…

    From the Rails codebase, the only place that mentions handling an :on option is in the validations module code in ActiveModel::Validations.

    If you look through the ActiveRecord::Callbacks code, you’ll see that there’s no mention of :on, nor does the ActiveRecord::Callbacks module include any of the ActiveModel::Validations module that will handle that option. There is an include for ActiveModel::Validations::Callbacks, but that will just provide the definitions for the before_ and after_ validations methods. However, the before_validation and after_validation callbacks will handle the :on option as seen here in their definitions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get two form validation run statement in my project.First I want
I want to run n fold cross validation on some classifiers I am using.
I found this script from a tutorial but I don't want the validation part,
After I run .validate(), I want to do something if the validation fails. $(document).ready(function
I want to do validation for EditTextCell widget in the grid. So I am
I want to perform simple validation against multiple fields. Please note these fields are
I want to call a validation method inside a shared gwt class that i
I want to create a validation range in cell A1. This validation allow user
I want to do some client side validation using javascript in ASP.NET page. I
I want DefaultModelBinder not to perform any validation based on DataAnnotations metadata. I'm already

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.