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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:39:50+00:00 2026-05-31T14:39:50+00:00

Is there a way to run code BEFORE model retrieval? I know about the

  • 0

Is there a way to run code BEFORE model retrieval? I know about the after_find callback, but I need to run before. I’d also like it to run only ONCE per retrieval regardless of the number of records returned. Looking at the RoR source it seems the query is actually executed in exec_queries(or to_a) in ActiveRecord::Relation. Do/Should I override this method to add this hook?

And just in case I’m going about this all wrong, the reason I’m asking is I have an external REST API I am using to retrieve data, but it is too slow to retrieve after every page reload. I was originally using memcached, but I figured I could just use ActiveRecord to cache the data in a database so I can easily query the data and possibly join it with similar data from other REST APIs. I’d like to plug in a callback that would after a certain timeout duration, reload the data from REST before returning ActiveRecord results.

Basically I’m looking for the best way to centralize refreshing my database from another source (REST) instead of cluttering up my controllers or overriding every model accessor that I use (is there a way to override them all easily?). Perhaps the best solution lies here.

It appears all of the built in methods like all, find, first, and last call apply_finder_options (and then where), but the dynamically created finders (find_by_name, etc) call find_dynamic_match which eventually calls where. This is what lead me to the to_a method on the relation, since it is common and called when the query is actually executed, and not just when building a relation before a query is executed. However getting this low level in into Rails makes me uncomfortable.

It seems like my problem shouldn’t be an uncommon one, so perhaps my approach is wrong?

FYI, I’m new to rails and ruby. Thanks!

  • 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-31T14:39:52+00:00Added an answer on May 31, 2026 at 2:39 pm

    I would strongly advise against using a low-level hook in place of explicit cache checking. ActiveRecord has it’s own caching mechanism, but if that isn’t doing it for you and you need to build your own – use it explicitly before using ActiveRecord finders. Hooks like these can make it very confusing to determine what is happening and why, and is not a recommended practice. Here is an example using a proxy model:

    class CacheProxy
      attr_accessor :klass
      def initialize(klass)
        @klass = klass
      end
    
      def method_missing(method_id, *arguments, &block)
        reload_if_necessary
        klass.send(method_id, *arguments, &block)
      end
    
      private
      def reload_if_necessary
        return unless needs_reload?
        # perform reload
      end
    
      def needs_reload?
        # determine if we need to reload the cache
      end
    end
    
    class ActiveRecord::Base
      def self.proxy
        CacheProxy.new(self)
      end
    end
    

    Now you can even do MyModel.proxy.find_by_first_name_and_last_name('John', 'Doe')

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

Sidebar

Related Questions

Is there a way to run a function/callback before a model is saved using
Is there a way to run plain c code on top of the JVM?
Is there a way to run some sort of code analysis to find code
Is there any way I could run the following 'logical code' to actually work?
Is there a way to periodically run some code on app engine? I have
Using the built-in Ruby Minitest framework, is there a way to run some code
Is there any way to run some code after transaction commit in Django? I
I need a place to run an initialization code that is application specific (like
Is there any way in Kohana 3's ORM to run a chunk of code
Is there a way run Emacs from a USB drive? I am a Windows

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.