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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:54:39+00:00 2026-05-24T21:54:39+00:00

I was playing around with implementing Rails model callbacks (after_save, before_save) etc. using alias

  • 0

I was playing around with implementing Rails model callbacks (after_save, before_save) etc. using alias method. All it does is it aliases the save method to save_with_callbacks. It works, except before_save has to be called after save is defined or alias keyword throws an error. I’m still in the process of understanding how Rails callbacks really works, but was wondering if there’s a way to use before_filter anywhere in the model.

module ClassMethods
  def before_save
    class_eval do
      # old_save points to save
      # save points to save_with_callbacks
      alias :old_save :save
      alias :save :save_with_callbacks
    end
  end
end

module InstanceMethods
  def save_with_callbacks
    @save_with_callbacks_text = 'Saving with callbacks'
    old_save
  end
end

class Task
  extend ClassMethods
  include InstanceMethods

  attr_reader :save_text, :save_with_callbacks_text

  def save
    @save_text = 'Saving'
  end

  # Needs to be called after save, save_with_callbacks are defined
  before_save
end
  • 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-24T21:54:39+00:00Added an answer on May 24, 2026 at 9:54 pm

    I forgot users don’t generally define ‘save’ method, but let ORM do it for you. Moved save method to InstanceMethods and that solves the problem.

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

Sidebar

Related Questions

Ok so I'm playing around with a nested comments system on rails using Awesome
I'm playing around with LinqToSQL using an existing multi-lingual database, but I'm running into
So, I'm just playing around implementing some sorting algorithms in C++, but I'm finding
I'm playing around with the idea of using Git for deployment and updates of
Playing around with generating text randomly with each page refresh using php. Is there
Just playing around with java trying to learn it etc. Here is my code
I am playing around with implementing a REST service on the Google AppEngine. I
After playing around with links in Rails for a view hours i've managed to
I'm playing around with implementing an event pool pattern in my javascript, similar to
Playing around with Python - tkInter - Entry widget - when I use validatecommand

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.