Home > OS >  Ordering table containing boolean
Ordering table containing boolean

Time:10-27

I'm looking to order my table alphabetically, but with data that is active first, then alphabetically where active is false after this. I am doing this through a query in my table but am unsure how to actually do this. As it stands right now the query simply orders alphabetically by the long name. Note: The table contains a long_name column which is a string and an active column which is a boolean.

Query used in controller:

@arrest_reason = ArrestReason.order(:long_name)

CodePudding user response:

Just add the boolean column to the order clause too:

@arrest_reason = ArrestReason.order(active: :desc, long_name: :asc)

I guess that your database encodes false as 0 internally and true as 1. Therefore, you have to order the boolean columns descending when you want to have the active records first.

  • Related