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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:35:04+00:00 2026-06-06T14:35:04+00:00

I have a Rails engine that defines a class method top(count) on a model

  • 0

I have a Rails engine that defines a class method top(count) on a model of choice. What this method does is grab count IDs from a sorted set (ZSET) in Redis. The idea is that each item of this model has a score, and this method is to return the “best” records. The method essentially looks like this:

def self.top(count = 1)
  ids = redis.zrevrange(self.score_set, 0, count - 1)
  items = self.find ids

  if count == 1
    return items.first
  else
    return items.sort { |x, y| ids.index(x.id) <=> ids.index(y.id) }
  end
end

As you can see, I currently sort the records after the fact because databases (or the adapters) tend not to maintain order when finding by multiple IDs. For instance, Model.where('ID IN (?)', ids) will sort by ascending IDs rather than the order of the passed IDs.

Sorting the items returns an Array, but I’d like this method to return an ActiveRecord::Relation instead so that my users can chain it with other ActiveRecord query methods. Is there a way I can use the built-in ActiveRecord query methods to maintain order from my array of IDs? Or alternatively, is there a way to construct an ActiveRecord::Relation from an array of records?

  • 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-06T14:35:05+00:00Added an answer on June 6, 2026 at 2:35 pm

    It really depends on the underlying database you are using. I do not think rails has a pretty way to do this.

    I have an application where I am doing the same thing as you. Storing ids in a Redis ZSET and then needing to fetch them in that order. Since we are using MySql we can take advantage of the field function.

    Model.where('id in (?)', ids).order("field(id, #{order})")
    

    PS: Make sure your order variable is sanitized!

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

Sidebar

Related Questions

So, imagine I have this model: class Car has_one :engine end and the engine
I have a rails helper in my application_helper.rb file that looks like this: def
I created a Rails 3 engine that is namespaced. Within my engine, I have
I have a micro-mini-search engine that highlights the search terms in my rails app.
I have a Rails engine (someone else's gem) that I am trying to modify
I have a Rails 3 app that I am turning into a Rails engine
I'm writing a blog engine in rails, and have set up a tag model
I have an engine, with this routes file: Rails.application.routes.draw do resources :comments, :controller =>
The context : I have a rails engine (gem) that uses omniauth inside it
I have a Rails 3 engine gem that is for basic user authentication and

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.