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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:38:58+00:00 2026-05-17T16:38:58+00:00

In a stats part of a Rails app, I have some custom SQL calls

  • 0

In a stats part of a Rails app, I have some custom SQL calls that are called with ActiveRecord::Base.execute() from the model code. They return various aggregates.

Some of these (identical) queries are run in a loop in the controller, and it seems that they aren’t cached by the ActiveRecord query cache.

Is there any way to cache custom SQL queries within a single request?

  • 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-17T16:38:59+00:00Added an answer on May 17, 2026 at 4:38 pm

    Not sure if AR supports query caching for #execute, you might want to dig in the documentation.
    Anyway, what you can do, is to use memoization, which means that you’ll keep the results manually until the current request is over.

    do something like this in your model:

    def repeating_method_with_execute
       @rs ||= ActiveRecord::Base.execute(...)
    end
    

    This will basically run the query only on the first time and then save the response to @rs until the entire request is over.

    If I am not wrong, Rails 2.x already has a macro named memoization on ActiveRecord that does all that automatically

    hope it helps

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

Sidebar

Related Questions

I have a rails app that I have serving up XML on an infrequent
I have a rails partial that renders a sidebar for users. As part of
I have a few Munin plugins which report stats from an Autonomy database. They
I'm working on a rails app that has a whole bunch of before filters
I have a perl script that will perform some operations on directories, and I
I'm trying to use Twitter's Oauth Single Token for posting from a rails app
Imagine a web application written in Ruby on Rails. Part of the state of
I am building stats for my users and dont wish the visits from bots
I am looking for some stats on current browsers for how long it takes
I have an ASP.NET web site dedicated to reporting on PBX extension stats. It

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.