7

I made a strategic mistake when developing database architecture on my Rails app

Now I have to implement sorting by price feature using

MyModel.order('price DESC')

price is a string type in the database, which cause 50 to be greater than 2000 for example

Are there any ways to implement such .order() without changing database structure?

EDIT:

I switched to correct type (integer) for price column. It took me an hour only to refactor.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
Sergey Khmelevskoy
  • 2,429
  • 3
  • 19
  • 43

1 Answers1

17

With PostgreSQL you will want to cast your string to integer/float/decimal (after you decide you 100% will not go and change the column type to correct one):

MyModel.order('price::integer DESC')

Consider this answer to make it work fast.

Community
  • 1
  • 1
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145