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

Ask A Question

Stats

  • Questions 148k
  • Answers 148k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would choose one test and change it to require… May 12, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer Google is your buddy. :) http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIPickerView_Class/Reference/UIPickerView.html#//apple_ref/doc/uid/TP40006842-CH3-SW10 May 12, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer WCF supports duplex HTTP bindings. As long as the initiating… May 12, 2026 at 9:20 am

Related Questions

I have been looking at various dependency injection frameworks for .NET as I feel
I am working on a little pinball-game project for a hobby and am looking
I have an ASP.NET application that uses a layered architecture e.g. presentation layer, business
I have an initialization class that preloads content into a variable (probably a list

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.