class Resource
def self.find_by_ordered_ids(*array)
options = {}
options = array.pop if array.last.is_a? Hash
# pass an Array or a list of id arguments
array = array.flatten if array.first.is_a? Array
find(array, options).sort_by {|r| array.index(r.id)}
end
end
(Note, untested, but that doesn’t mean that it won’t work
Ah, now that is a great tip, i’d not seen this before! Thanks a lot.
The weird thing is, i’ve gone to my console to try it out, and rails is
now always giving me the resources back in the order of the id array
anyway, whether i use the field option or not. It’s the same with any
of my AR classes! I’m sure that this didn’t used to happen though…i
think i’m going crazy. Can anyone tell me if this is normal? If so
then i just imagined this whole problem…
The weird thing is, i’ve gone to my console to try it out, and rails is
now always giving me the resources back in the order of the id array
anyway, whether i use the field option or not. It’s the same with any
of my AR classes! I’m sure that this didn’t used to happen though…i
think i’m going crazy. Can anyone tell me if this is normal?
You are not going crazy.
MySQL may, but is not required to, return the objects in the order of
the array. So, the behavior will change without warning.
i think i’m going crazy. Can anyone tell me if this is normal?
You are not going crazy.
I am! This is a reply to a two-year-old thread… I just wasted 10
minutes trying to find out why I hadn’t received the previous
messages. I thought my email spam filter was being paranoid :-/
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.