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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:29:42+00:00 2026-06-09T04:29:42+00:00

Looks like Heroku cache scope result, and my questions is how they decide when

  • 0

Looks like Heroku cache scope result, and my questions is how they decide when to cache, when to refresh the scope result and how to use scope correctly.
In my code, I have a name scope for the Question model like following

class Question < ActiveRecord::Base
  scope :today, where('show_date = ?', Time.now.localtime("-07:00").strftime("%Y-%m-%d"))  
end

And it always starts to give me the same cached result. My Guess is if I change Question Data, the Result will probably get updated.
A quick research give me a related topic: Cached named_scope on Heroku?.

I already changed the code to be:

class Question < ActiveRecord::Base
  def self.today
    today_string = Time.now.localtime("-07:00").strftime("%Y-%m-%d");
    where('show_date=?', today_string)                                                                                                                 
  end
end

I guess this will always give me the correct result without any cache. But I an still wondering is there a better way to benefit from scope cache while get the correct result at the same time? 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-06-09T04:29:44+00:00Added an answer on June 9, 2026 at 4:29 am

    Scopes are set when the application is first loaded so in the first case your scope query will be fixed to date at that time, until the application is restarted for whatever reason. It is the query definition rather than the query results that are getting cached here. Generally if you have a condition in your scope that is time dependent then your need to use a lambda to wrap the query definition within your scope, e.g.:

    class Question < ActiveRecord::Base
      scope :today, lambda { where('show_date = ?', Time.now.localtime("-07:00").strftime("%Y-%m-%d")) }
    end
    

    That will cache the scope definition but will execute the body of the lambda every time it is called. It will not cache the results of the query.

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

Sidebar

Related Questions

looks like heroku is using npm version 1.0.94 I have a dependency that require
Right now I have a submit button and the code looks like this <%=
Looks like operator new and operator new[] have exactly the same signature: void* operator
It looks like this block directly flushes the output. What's the practical use of
I have a Rails migration that looks like this: puts *** What would you
I have partial view that displays model-specific flash messages. The partial looks like: app/views/mymodel/_flashpartial.erb
I am about to launch a beta site, and heroku looks like a great
I've been having problems yesterday with Heroku shared Postgres DB. It looks like my
Looks like this question has been asked thousand times already, but each person's configuration
Looks like while( condition ) { //do stuff } is completely equivalent to for(

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.