views:

58

answers:

2

I have a has_many association like this:

has_many  :respostas_matriz_alternativas, :class_name => 'RespostaMatrizAlternativa',
          :order => 'respostas_matriz.codigo asc',
          :include => :resposta_matriz

Well the fragment that matters is the "order". Codigo is a varchar column wich will contain in most cases numeric values. To show this data I need to order it by code, but when I have only numbers the order becomes awkward, something like:

1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, ...

What do you suggest for me to solve it?

Thanks.

+3  A: 

I don't know that there's much you can do if the column will contain a mix of strings and numbers, but if the column will always be numeric you could use something like:

:order => 'cast(respostas_matriz.codigo as unsigned) asc'
palmsey
A: 

I'm kind of just a beginner with Ruby, but could you use something like the following to split the string into an array of smaller strings, then convert each string to an integer.

I don't have my rubydoc in front of me, but something like

:order => 'respostas_matriz.codigo.split.map {|s| s.to_i}'
b3ng0