E
E
eldar_web2016-01-25 11:33:55
Ruby on Rails
eldar_web, 2016-01-25 11:33:55

How can I sort numbers in string types in Ruby on Rails (MySQL database)?

There is a table. And there is a string type field, for example, house_number.
This field usually stores numbers.
And I want to sort this field so that the numbers go in order ( .order(:house_number) ).
But this does not happen, because the field is of string type.
Are there solutions to this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
N. Bekseitov, 2016-01-25
@eldar_web

order('cast(house_number as unsigned) asc')

E
Evgeny Kungurov, 2016-01-25
@evgenykungurov

For example, Model.all.sort_by { |instance|| instance.house_number.to_i }
didn't really check, no rails at hand

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question