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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:09:44+00:00 2026-05-27T07:09:44+00:00

I am creating a gem which create hooks like before_update_filter in ActiveRecord class .

  • 0

I am creating a gem which create hooks like before_update_filter in ActiveRecord class .
For this I have create a module UpdateFilter as given below :

module UpdateFilter
 def before_update_filter(*args)
    puts self #in class scope
    self.set_callback(:save, :before, args[0].to_sym)
 end
end

And in intializers I am doing

ActiveRecord::Base.extend UpdateFilter

Ok. Now all above is working fine . But I want to set_callbacks only if some attributes of instance changed and I cannot access attributes in before_update_filter method because it is in class scope .

As a summary I want to implement hook like . I hope it clears what I am trying to do .

before_update_filter :instance_mthod_name, :attr_prams => [:name, :rating]

Now how can I implement this??

  • 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-27T07:09:45+00:00Added an answer on May 27, 2026 at 7:09 am

    It might require some edits, but I think that should work:

    module UpdateFilter
     def before_update_filter(callback_method, options = {})
        puts self # class scope
        self.set_callback :save, :before do
          puts self # instance scope
          # check if all of the listed attributes have changed
          if options[:attr_params].map{ |attr| attribute_changed?(attr) }.all?
            # call instance method
            send callback_method
          end
        end
     end
    end
    

    According to the docs block passed to set_callback is executed in the context of the current object. So we have access to all instance methods inside this block. We check that all (replace it with ‘any’ or even some other conditions) listed attributes have changed and only then call required callback instance method.

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

Sidebar

Related Questions

This code was taken from ActiveRecord 2.3.14's gem class ConnectionHandler def establish_connection(name, spec) @connection_pools[name]
I'm creating a gem which has several scripts in the bin directory the utility
I am in the process of creating a gem which needs to add associations
which is the best gem for creating a login system?
MY team and I are working on creating gem libraries that have various external
Typically I create a plugin when I have a module that I know I'm
I am creating what I expect to be ruby gem. Anyone have a good
I have three models: class Client < ActiveRecord::Base has_many :balancesheets has_many :investment_assets, :through =>
I would like to ask if someone knows any good gem for creating nice
I want to use this gem in my api application https://github.com/seangeo/auth-hmac/ I have a

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.