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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:04:13+00:00 2026-06-01T16:04:13+00:00

Suppose I have a non-empty array ids of Thing object ids and I want

  • 0

Suppose I have a non-empty array ids of Thing object ids and I want to find the corresponding objects using things = Thing.find_all_by_id(ids). My impression is that things will not necessarily have an ordering analogous to that of ids.

  1. Is my impression correct?

  2. If so, what can I used instead of find_all_by_id that preserves order and doesn’t hit the database unnecessarily many times?

  • 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-01T16:04:14+00:00Added an answer on June 1, 2026 at 4:04 pm
    1. Yes
    2. Use Array#sort

    Check it out:

    Thing.where(:id => ids).sort! {|a, b| ids.index(a.id) <=> ids.index(b.id)}
    

    where(:id => ids) will generate a query using an IN(). Then the sort! method will iterate through the query results and compare the positions of the id’s in the ids array.

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

Sidebar

Related Questions

Suppose I have my own non-inline functions LockMutex and UnlockMutex, which are using some
I want to detect Empty paragraphs in Word Document using Microsoft.Office.Interop.Word. Suppose, if my
Let`s suppose we have a simple (non-assoc) array with 100001 values and these values
Suppose I have a list of items (e.g., Posts) and I want to find
Suppose I have a function which can either take an iterable/iterator or a non-iterable
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Suppose I have a method which changes the state of an object, and fires
Suppose I have have made a an osX app without using Xcode. After compiling
Suppose I have a 2d sparse array. In my real usecase both the number
Suppose I have a weighted non-directed graph G = (V,E). Each vertex has a

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.