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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:29:28+00:00 2026-06-10T01:29:28+00:00

I wrote a simple Cacheable module that makes it simple to cache aggregate fields

  • 0

I wrote a simple Cacheable module that makes it simple to cache aggregate fields in a parent model. The module requires that the parent object implement the cacheable method and a calc_ method for each field that requires caching at the parent level.

module Cacheable
  def cache!(fields, *objects)
    objects.each do |object|
      if object.cacheable?
        calc(fields, objects)
        save!(objects)
      end
    end
  end

  def calc(fields, objects)
    fields.each { |field| objects.each(&:"calc_#{field}") }
  end

  def save!(objects)
    objects.each(&:save!)
  end
end

I would like to add callbacks to the ActiveRecord model into which this module is included. This method would require that the model implement a hash of parent models and field names that require caching.

def cachebacks(klass, parents)
  [:after_save, :after_destroy].each do |callback|
    self.send(callback, proc { cache!(CACHEABLE[klass], self.send(parents)) })
  end
end

This approach works great if I manually add both callbacks using such as:

after_save proc { cache!(CACHEABLE[Quote], *quotes.all) }
after_destroy proc { cache!(CACHEABLE[Quote], *quotes.all) }

But, I’m receiving the following error when I try to use the cachebacks method to add these to callbacks.

cachebacks(Quote, "*quotes.all")

NoMethodError: undefined method `cachebacks' for #<Class:0x007fe7be3f2ae8>

How do I add these callbacks to the class dynamically?

  • 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-10T01:29:30+00:00Added an answer on June 10, 2026 at 1:29 am

    This looks like a good case for ActiveSupport::Concern. You can tweak your cachebacks method slightly to add it as a class method on the including class:

    module Cacheable
      extend ActiveSupport::Concern
    
      module ClassMethods
        def cachebacks(&block)
          klass = self
          [:after_save, :after_destroy].each do |callback|
            self.send(callback, proc { cache!(CACHEABLE[klass], *klass.instance_eval(&block)) })
          end
        end
      end
    
      def cache!(fields, *objects)
        # ...
      end
    
      # ...
    end
    

    To use it:

    class Example < ActiveRecord::Base
      include Cacheable
      cachebacks { all }
    end
    

    The block you pass to cachebacks will be executed in the context of the class that’s calling it. In this example, { all } is equivalent to calling Example.all and passing the results into your cache! method.


    To answer your question in the comments, Concern encapsulates a common pattern and establishes a convention in Rails. The syntax is slightly more elegant:

    included do
      # behaviors
    end
    
    # instead of
    
    def self.included(base)
      base.class_eval do
        # behaviors
      end
    end
    

    It also takes advantage of another convention to automatically and correctly include class and instance methods. If you namespace those methods in modules named ClassMethods and InstanceMethods (although as you’ve seen, InstanceMethods is optional), then you’re done.

    Last of all, it handles module dependencies. The documentation gives a good example of this, but in essence, it prevents the including class from having to explicitly include dependent modules in addition to the module it’s actually interested in.

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

Sidebar

Related Questions

I wrote a simple function, that makes emacs add matching quotes (so when I
I wrote simple class that on the start it just increase the value of
I wrote simple application that using the cell phone camera. When i trying to
I wrote a simple webapp using google app engine in python that allows users
I wrote simple benchmark that test performance of multyplying doubles vs BigDecimal. Is my
I wrote simple update/insert statements that are returning a syntax error, what am I
I wrote simple application that send some data to my server. Somehow the data
While checking LINQ's abilities I've wrote simple QuickSort implementation and was glad that ultimately
I wrote a simple tool to generate a DBUnit XML dataset using queries that
I wrote a simple program that using Class::ArrayObjects but It did not work as

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.