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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:22:14+00:00 2026-05-27T01:22:14+00:00

This should be a simple problem, but I just can’t seem to find a

  • 0

This should be a simple problem, but I just can’t seem to find a good solution. I have a list of id’s that the user is selecting, and I want to preserve that order for the foos.

foo_ids = [1899, 7, 1, 2, 3, 42]

foos = Foo.find(foo_ids)

Any ideas on how to either preserve the order on select, or resort after the result set is returned?

  • 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-27T01:22:15+00:00Added an answer on May 27, 2026 at 1:22 am

    I don’t think there is a way to do that with ActiveRecord.

    However, here is what I would do:

    people_ids = [1899, 7, 2, 3, 42]
    
    people = Person.unscoped.find(people_ids)
    
    people_ids.each_with_object(result = []) do |id, result|
      result << people.detect { person.id == id }
    end  
    
    result # => what you are looking for
    

    I use Person.unscoped because in any case, your default scope is not the one you want. So it may be a little (very little) bit faster.

    Using find(people_ids) produce only one query no matter how big is people_ids.
    The worst would be to do something like this:

    people_ids.each_with_object(result = []) do |id, result|
      result << Person.find(id)
    end
    

    Because it produces a query for every id.

    Sorry not to have the perfect answer 😉

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

Sidebar

Related Questions

I have been looking everywhere but can't seem to find a good solution for
This should seem simple enough, but can't figure it out. I was porting a
This should be a simple question, but I just can't recall the relevant API.
This should be a simple problem, but I am have a tough time getting
This should probably have a simple answer I just can't figure out. Anyway, I
This problem is very simple, but I just can't figure it out added to
The problem itself is simple, but I can't figure out a solution that does
This should be fairly simple from what I can find online, but I can't
This should be a simple question, but I can't seem to figure it out.
Hi all I have a (probably) simple problem but I just can't figure out

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.