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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:15:54+00:00 2026-05-29T06:15:54+00:00

So right now users have a list of items that changes, but not too

  • 0

So right now users have a list of items that changes, but not too often, so I decided to keep it in a cache like so:

  #Fetch the cache
  def self.fetch_cache(user)
    Rails.cache.fetch(user.items, :expires_in => 15.minutes) do
      user.items.order("created_at ASC").all
    end    
  end

This works if the user has items. The problem is, if the user doesn’t have any items (because they’re a new user or something) and the cache will return an error.

How should I be using this to make it work for users with nothing?

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-29T06:15:55+00:00Added an answer on May 29, 2026 at 6:15 am

    You’re using a really weird cache key there (the first argument to Rails.cache.fetch). You’re supposed to be passing a string that identifies the cache entry but you’re passing in an association.

    Rails is probably helpfully calling to_s on user.items in order to turn it into a string that but that’s not a useful cache key as you’re going to have to load the user’s items in order to compute the cache key to get the users’ items from the cache!

    Something like "user_#{user.id}_items" would be a far better cache key.

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

Sidebar

Related Questions

Users on my site can post news items. But right now, it's all honor
Right now we have AD/Exchange to manage all of our users logins/e-mail on-site at
We have a script right now which our Windows users run on a Linux
Right now, I keep all of my projects on my laptop. I'm thinking that
I have next link that lets users navigate through a list of URL (stored
Right now I have a calender that only displays one month(what ever month I
How can I validate the user input by using scanf. Right now I have
My website shows recommend pages for users. Right now, I suppose to send a
Rails newbie here. I have a list of items which can have a status
I have a selection list that is generated dynamically, it lists a number of

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.