Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ordering table containing boolean

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading