I have the following code:
@selected_authors = Author.find(:all, :conditions => ["AUT_OID in (?)",@selected_authors_array ])
@selected_authors_array is an array of author ids e.g: 55 75 74
Note the ids in the array are not in a particular order
When I do
<%@selected_authors.each do |author| %>
<%=author.AUT_OID %> -- <%=author.AUT_NAME %>
<% end %>
It displays 55, 74, 75 (it changes the order of the ids to ascending order)
I would like to keep the order of the entries in @selected_author as it is in the array ‘@selected_authors_array’.
Any help is most appreciated
It’s an issue of the underlying database.
If you do a search/comparison on the column AUT_OID it will automatically use this index (if available) or the primary one to go though all entries.
If the comparison returns “true” the DB will just pop the current row to the result list.
Thus, you get an AUT_OID-ordered list if you have an index/primary on AUT_OID and do a search on it.
I think you need to resort it manually if you really need the same order of IDs.
Tipp: On mySQL you can see which index (and thus the order) the db is using by using the EXPLAIN syntax: