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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:42:15+00:00 2026-05-17T19:42:15+00:00

For a Ruby on Rails app, I’m having this Movie object which has several

  • 0

For a Ruby on Rails app, I’m having this Movie object which has several attributes. Pretty much every page needs to compare some movies to a full list of these movies sorted by ratings, so I used to cache this big sorted list. It was useful, because I could directly use this list of Movies and all their attributes without any other DB request.

Recently, the list got bigger and a big array of 2500 movies just can’t fit into a 1MB Memcache chunk.

I was wondering if splitting this Movie object into a smaller object (id, created_at, updated_at, score, attributes_id) and externalizing its attributes into another object was a good idea. I would have a smaller list of movies sorted by score, but lot of other requests.

While we’re at it, why not even cache an array of 2500 integers, do the maths with Ruby and only get the 50 movies we’re interested in? Would it require 50 DB calls, or can I ask PostGreSQL to give me an array of the movies 2, 5, 6, 89, …, and 2467?

What should I do? What other part of the problem am I missing?

Thanks you,

Kevin

  • 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-17T19:42:15+00:00Added an answer on May 17, 2026 at 7:42 pm

    Instead of stripping attributes out of your model, why not load in only what you need? It’s often to your advantage to use the data directly instead of involving the models when doing simple numerical comparisons. The storage requirements for a simple hash are dramatically lower than a full ActiveRecord instance.

    In this case, select_all is your friend:

    class Movie < ActiveRecord::Base
      def self.data_for(*ids)
        select_all(
          sanitize_sql([ "SELECT id, score FROM #{quote_table_name} WHERE id IN (?)", ids.flatten ])
        ).inject({ }) do |h, row|
          h[row[:id].to_i] = row
          h
        end
      end
    end
    

    This will produce a simple hash that looks something like this:

    Movie.data_for(2, 5, 6, 89, 2467)
    # => { 2 => { :id = > '2', :score => '4' }, ... }
    

    You can clean up the structure if you like to convert strings to integers as required, and this will further decrease your storage requirements. You can store a lot of data with plain numbers and simple hashes.

    Stashing this hash in Memcache is, at this point, a simple exercise of using Rails.cache

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

Sidebar

Related Questions

I am busy making a ruby on rails app. I want to make an
Some of the users of our Ruby on Rails app have complained that page
I am trying to design my ruby on rails app at the moment. I
I have a Ruby on Rails (2.3.5) application and an APE (Ajax Push Engine)
I starting to investigate whether Ruby-on-Rails will help solve my problem in some way?
So I'm finally ready to allow some friends to use my Ruby on Rails
I am wondering how to integrate node.js on a rails app (for learning purpose).
Couldn't find this anywhere, maybe I'm looking for the wrong verbs. I'm trying to
Currently, I put the keys I use to access other API's and the like
I want to have an ability to set a custom message in a model

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.