I am constructing a resultset using,
@time_spent = TimeEntry.find(:all,
:joins => "INNER JOIN sometable ON x = y",
:select =>"id, subject, spent_on")
And to get column names i am using
@time_spent[index].attributes.keys
But it gives me column names in some random order. How can i get in the order same as in select clause.
Can any one tell on what basis elements are ordered when using attributes.keys?
Any hints would be helpful.
Thnx.
The command
Returns a regular Ruby hash. For ruby 1.8, according to the docs:
In ruby 1.9, this has changed, and now according to the docs “Hashes enumerate their values in the order that the corresponding keys were inserted.”
If you really wanted to learn more, you’d have to dig into the internals of your Ruby version, but there’s not much point in that. In ruby 1.8, you can pretty safely expect the keys to come out in an un-helpful order for you, and if you need them in some sort of specific order, you will need to implement that yourself.