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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:15:46+00:00 2026-06-04T04:15:46+00:00

As you know, before_save callbacks are executed prior to before_create callbacks. Therefore, some people

  • 0

As you know, before_save callbacks are executed prior to before_create callbacks.

Therefore, some people have suggested that in would be better to use before_save :method, :on => :create instead of before_create so that the callback method is executed at the right time in relation to other callbacks (such as autosave callbacks). See, for example, this Pivotal Labs blog post, and this StackOverflow answer.

However, as far as I can tell, the :on => :create option does not achieve the desired effect on a before_save callback. In other words, the callback is executed for every save regardless of whether it is a create or not.

The :on => :create option does appear to be valid for before_validation callbacks, though.

Could someone confirm whether the :on => :create is supposed to work for a before_save? Did it work in previous versions of Rails and is now broken, or are the aforementioned links simply mistaken?

Assuming :on => :create is not valid, is the following acceptable, and/or is there a better way?

before_save :callback_method, :if => :new_record?

Thank you.

  • 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-04T04:15:48+00:00Added an answer on June 4, 2026 at 4:15 am

    You’re right, there is no :on option for before_save callback.
    But, I don’t understand, why use before_save instead of before_create.
    before_create callback will be called right after before_save.

    Of course, you can use before_save :callback_method, :if => :new_record?. But I personally don’t like this solution – what if I need to add conditions in the :if option?

    If one have a dependency between before_save and before_create callbacks, I`d suggest, to combine 2 callbacks. For instance (pseudocode):

    class MyModel < ActiveRecord::Base
      before_create :prepare_x
      before_save :do_something_with_x
    
      def prepare_x
        @x = 10
      end
    
    
      # will not work, because `prepare_x` called after `do_something_with_x`
      def do_something_with_x
        @a = 100 / @x
      end
    end
    
    # ||
    # ||
    # \/
    
    class MyModel < ActiveRecord::Base
    
      before_save :do_something_with_x
    
      def do_something_with_x
        @x = 10 if new_record?
        @a = 100 / @x
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know we've done this before in another .aspx page that's using this master
I know that I've seen this site before, but cannot remember it for the
I've created an Observer for the User class (below). I know the before_save method
I have the following piece of code (that may be invoked multiple times or
We have small class, that has a related class class Car has_one :engine, :dependent
I have a database that stores an arbitrary number of phone numbers. There are
In my rails app I have a User model. In that model I have
Say that I have two models- Users and Accounts. Each account can have at
I have an invoice model, that has many invoice items. I have a form
I have a standard polymorphic relationship and I need to know who its parent

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.