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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:57:37+00:00 2026-05-10T21:57:37+00:00

I have a class in which looking up an instance is expensive, so instances

  • 0

I have a class in which looking up an instance is expensive, so instances are cached:

class Foo   def self.find(id)     Rails.cache.fetch('Foo.#{id}') do       // some expensive lookup, like an HTTParty request, or a long SQL query       ...     end   end end 

That works fine until Foos have related Foos:

class Foo   def children     @child_foo_ids.map { |id| Foo.find(id) }   end end 

I’d like to use ||= caching to save repeated trips:

class Foo   def children     @children ||= @child_foo_ids.map { |id| Foo.find(id) }   end end 

But Rails.cache freezes the found Foos, so I can’t set the instance variable after I create and cache the object. (i.e. this method raises a TypeError.)

One solution would be to pre-fetch the parent when I first do the expensive find, but that could end up loading a gigantic object graph when I only want one or two instances.

  • 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. 2026-05-10T21:57:38+00:00Added an answer on May 10, 2026 at 9:57 pm

    You can use ||= caching; you just have to use a little indirection:

    class Foo   def initialize     @related_objects = {}   end   def children     @related_objects[:children] ||= @child_foo_ids.map { |id| Foo.find(id) }   end end 

    Rails.cache won’t freeze the instance variables of each Foo, so that Hash can be modified!

    PS: yes, I did just post this question and the answer at essentially the same time. I figured the community could benefit from my struggle.

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

Sidebar

Related Questions

For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
I have a class which is not thread safe: class Foo { /* Abstract
I have a method which should consider a collection of instances of a class
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which looks something like this: public class Test { private
I have a class which constructor takes a Jakarta enums . I'm trying to
I have a class which inherits from QTreeWidgetItem and I intercept the click event.

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.