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

  • Home
  • SEARCH
  • 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 8786805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:43:50+00:00 2026-06-13T21:43:50+00:00

I am trying to cache some data from the database to reduce number of

  • 0

I am trying to cache some data from the database to reduce number of SQL queries to the database. Currently, what I am doing is that I will load a set of records from the database:

@records = Record.find(:all, :conditions => ["id < ?", 100])

and then iterate through this array to find the records that I really want:

@needed_records = Array.new
@records.each do |record|
  if record.is_needed
    @needed_records.push(record)
  end
end

So that whenever I need, i can just pass @records instead of actually accessing the database.

My question is, is there any shortcoming doing it this way? Is there any better way to do the equivalent?

  • 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-13T21:43:51+00:00Added an answer on June 13, 2026 at 9:43 pm

    Two ways I’d recommend:

    1) The better way would be to use conditions and scopes to filter the actual data you are looking for, instead of pulling and manually iterating over the records. This assumes you can translate “record.is_needed” into conditions or SQL to get desired results

    2) Use Rails’ Cache Store the store the results from the DB: http://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-store

    Rails.cache.write(:records, @records)
    

    Databases love to be queried… They get off on it!

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

Sidebar

Related Questions

I'm trying to cache results from several pages that do heavy database processing. These
I'm trying to cache a page that gets/processes data from very slow API's -
I'm trying to cache JSON content generated by a php script from database. However
I am trying to make an asynchronous request to get some data from my
I am trying to load an entity with byte data (annotated with @Lob) from
Currently i am trying to export the data from the data table to the
I'm trying to export some tables generated with PHP from MySQL data. I'm trying
I'm trying to make a request using dojo.xhrGet to grab some XML data from
I am experiencing an interesting issue when I am trying to load some data
I'm currently trying to pick a database vendor. I'm just seeking some personal opinions

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.