Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Find by array of ids in the same order?

279 views
Skip to first unread message

Sandeep Gudibanda

unread,
Feb 14, 2008, 2:27:44 AM2/14/08
to rubyonra...@googlegroups.com
Hi,

I am trying to perform:

@people = Person.find(10,14,5,1)

The RUBY is acting intelligent in fetching the rows, it is sorting my
array and fetching the corresponding rows: Essentially I am returned
with rows 1,5,10 and 14 in that order..

It is essential for me to retain the order, can I force this happen?

Regards,
Sandeep G
--
Posted via http://www.ruby-forum.com/.

Sandeep Gudibanda

unread,
Feb 14, 2008, 2:29:29 AM2/14/08
to rubyonra...@googlegroups.com
Realized it is MYSQL that is acting intelligent in sorting the array,
can i force this not to happen?

Mark Reginald James

unread,
Feb 14, 2008, 8:20:12 AM2/14/08
to rubyonra...@googlegroups.com
Sandeep Gudibanda wrote:
> Realized it is MYSQL that is acting intelligent in sorting the array,
> can i force this not to happen?

ids = [10,14,5,1]
@people = Person.find ids, :order => "field(id,#{ids.join(',')})"

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field

--
We develop, watch us RoR, in numbers too big to ignore.

Reply all
Reply to author
Forward
0 new messages