Raw sql and columns order

Hi, I’ve a model with the following method:

def self.run(sql)
return connection.select_all(sql)
end

so, the value returned is an array of hashes… the problen is that the
sql
command:

“select name, id from table”

and

select id,name from table"

returns the columns in the same order (name, id).

And in my view, I need to keep the order of the SQL command.

(Don’t ask why I’m doing raw SQL… it’s a long story). :slight_smile:

Thanks everybody.

Hashes in Ruby don’t have any order. There is an OrderedHash class in
Rails which may be worth looking at.

It may be possible to maintain the order if you go right down to the
database specific connection object, below the AR connection layer.
There may be a choice of hash or array based results.

-Jonathan.

Thanks,

That’s just what I ended doing, by using the ruby-mysql result set I
could
finally do what I wanted.

Matias.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs