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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:25:00+00:00 2026-06-11T14:25:00+00:00

I have an array of item ID’s, item_ids . I’m passing this array into

  • 0

I have an array of item ID’s, item_ids.

I’m passing this array into an ActiveRecord query to retrieve the corresponding items from the database: Item.where(:id => item_ids)

Is there any way to get the items returned in the same order as passed in via the item_ids array?

If it makes a difference, I’m on Ruby on Rails 3.0.12 with PostgreSQL as my database.

  • 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-11T14:25:01+00:00Added an answer on June 11, 2026 at 2:25 pm

    You can get the database to do the sorting and avoid multiple index calls, you just have to remember that an SQL ORDER BY orders by an expression, not a column:

    whens = item_ids.collect.with_index { |id, i| "when #{id} then #{i}" }.join(' ')
    items = Item.where(:id => item_ids).order("case id #{whens} end")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array built from the URL of a webpage. If an item
I have a Item[] _items array of items, where some of the items may
I have an accordion that binds data for each item from an array. I
I have an abstract parent class Item , from which different types of items
If I have a plist which is structured like this: Root Array Item 0
I have an array containing much more items than just this one. This is
I have array which gets from session, this is print_r of array print_r($_SESSION['wpsc_cart']); wpsc_cart
I have this following class: Class myclass{ public static $item_row_name = array( array('Item 1','Item_1','Help
I have an array item like this: var array = USA.NY[2]; // gives Albany
Say I have an array of items I want to split on (this is

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.