Forum: Ruby on Rails ActiveRecord.find([id array]) question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F62032781f74abf688e375074ee638ba?d=identicon&s=25 Ryan Mohr (ryanmohr)
on 2007-07-28 00:57
If you have a collection of ids, what's the best way to extract the
records from the database while maintaining the original id order?

Recently I noticed that a find statement like

% User.find([1, 2, 3])

will return the same thing as

% User.find([3, 2, 1])

...ignoring the order of the ids in the array completely.  In my case, I
have an array that is twenty ids long and I need the ordering preserved.
What's the best solution without having to loop through the ids and
select each record one by one?
Bef7ff8a0537495a1876ffebdc9f8e51?d=identicon&s=25 Lionel Bouton (Guest)
on 2007-07-28 01:06
(Received via mailing list)
Ryan Mohr wrote:
>
> ...ignoring the order of the ids in the array completely.  In my case, I
> have an array that is twenty ids long and I need the ordering preserved.
> What's the best solution without having to loop through the ids and
> select each record one by one?
>

It's a SQL limitation, you can't enforce any order unless you actually
use ORDER BY (:order parameter of ActiveRecord::Base.find).

You could create a hash 'id => position' from your original id array and
use it to sort the results according to their id.

Lionel
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-07-29 00:06
(Received via mailing list)
On Jul 27, 2007, at 6:57 PM, Ryan Mohr wrote:
>
> ...ignoring the order of the ids in the array completely.  In my
> case, I
> have an array that is twenty ids long and I need the ordering
> preserved.
> What's the best solution without having to loop through the ids and
> select each record one by one?
> --

You could search for past examples of this very question for
starters, but here's a technique that will give you the result that
you seek:

ids = [ 3, 1, 2 ]

results = User.find(ids)
sorted_results = results.sort_by {|u| ids.index(u.id) }

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
5e75f4b81923cd04a5c8fffa290be92e?d=identicon&s=25 Carl Johnson (carlivar)
on 2007-07-30 08:28
> ids = [ 3, 1, 2 ]
>
> results = User.find(ids)
> sorted_results = results.sort_by {|u| ids.index(u.id) }

How do you do pagination with the above? Specifically with the
will_paginate plugin? Is it possible? will_paginate is used in place of
find, yet the sort is happening after the find...

BTW, this may be a SQL limitation, but as far as I can tell from
development.log, Rails does still change the order of the array
(numerical ascending). Not sure why... that's certainly not expected
behavior. Unless perhaps it optimizes performance for the query?
5e75f4b81923cd04a5c8fffa290be92e?d=identicon&s=25 Carl Johnson (carlivar)
on 2007-07-30 08:56
> BTW, this may be a SQL limitation, but as far as I can tell from
> development.log, Rails does still change the order of the array
> (numerical ascending). Not sure why... that's certainly not expected
> behavior. Unless perhaps it optimizes performance for the query?

Whoops, sorry to reply to myself, but after looking further I realize I
am wrong about the above.

Still trying to solve the find/array/paginate puzzle though...

Carl
This topic is locked and can not be replied to.