How to order results by an existing boolean attribute first
问题 I have many records with a featured? attribute. I would like to query all, group them in featured and non-featured and if possible random them in between their groups (random and show the featured ones, then random the rest). Any idea how to do this on ActiveRecord? 回答1: Use rand() MySQL SELECT * FROM <TABLE_NAME> ORDER BY featured?, rand() RAILS 3 ModelName.order("featured, rand()") For Ex:- id featured 1 true 2 false 3 false 4 true 5 false 6 true i want all the featured with value true