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 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

This is more a general question but my particular case involves a ruby/rails app
My Ruby on Rails app is using LivePipe Modals. ( http://livepipe.net/ ) Everything has
In my Ruby on Rails app, I have a User table and a Foo
I am using ActiveScaffold in a Ruby on Rails app, and to save space
I'm in the process of maintaining a Ruby on Rails app and am looking
I'm looking at ways to deploy a Ruby on Rails app (running on JRuby)
I'm looking to run Redmine, a Ruby on Rails app, on a VPS windows
I have a Ruby script in my Rails app that I use to load
I'm building an app in Ruby on Rails, and I'm including 3 of my
I'm trying to write an app using Ruby on Rails and I'm trying to

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.