Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Please un-deprecate before_save, after_save, etc methods

#4834

We have been working to move from 2.3.3 to 3.0.0.beta4 recently and have run into this decision:

Base#before_save has been deprecated, please use Base.before_save :method instead. (called from <class:User> at app/models/user.rb

I assume this decision was designed to protect people from themselves; that is, where they use inheritance or mixins that result in multiple "def before_save" definitions, which results in them not getting the callbacks that they expect. So the Rails team is recommending that the other ways of registering callbacks are preferred.

IF so, this is just that - a recommendation - and should remain as such. It's a great thing to document and point out to people as a caveat. It's also a great thing for plugin others to abide by. However, the "def before_save" idiom has been in Rails for years, and is immensely powerful. Consider:

class Auto < ActiveRecord::Base
  def before_save
    # ...
  end
end

class Car < Auto
  def before_save
    super
    # ...
  end
end

The great thing about this approach is the developer is using features of the Ruby language. A while ago Yehuda posted a long blog post about how we need to go back to our Ruby roots, and this approach supports that.

Furthermore, you can get into just as much trouble with the symbol approach:

class Auto < ActiveRecord::Base
  before_save :set_defaults
  def set_defaults
    # ...
  end
end

class Car < Auto
  before_save :set_defaults
  def set_defaults
    # ...
  end
end

With this setup, the developer could mistakenly think both were going to get called; using the symbol method name approach obscures what is going on under the hood. You would still need to put "super" in set_defaults, which requires a good amount of explanation.

Please reverse this decision and allow us Rails users to keep the "def callback" approach which has proven to be so powerful in Rails over the past years.

Thanks,
Nate Wiger
PlayStation

Reported by Nate Wiger · June 11th, 2010 @ 08:41 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid
    • Importance changed from to Low

    I don't think before_save/after_save have been deprecated. I don't get any deprecation warnings.

    October 7th, 2010 @ 07:19 AM

  2. Michael Hasenstein
    Michael Hasenstein

    I do - Rails 3.0.4. I don't know if it's VALID, but I DO see the message. I changed the names of my before/after_save functions to something else, in my case naming was accidental and I don't care.

    February 18th, 2011 @ 10:08 AM