@@ -622,7 +622,8 @@ def order!(*args) # :nodoc:
622
622
self
623
623
end
624
624
625
- # Allows to specify an order by a specific set of values.
625
+ # Applies an <tt>ORDER BY</tt> clause based on a given +column+,
626
+ # ordered and filtered by a specific set of +values+.
626
627
#
627
628
# User.in_order_of(:id, [1, 5, 3])
628
629
# # SELECT "users".* FROM "users"
@@ -633,6 +634,32 @@ def order!(*args) # :nodoc:
633
634
# # WHEN "users"."id" = 3 THEN 3
634
635
# # END ASC
635
636
#
637
+ # +column+ can point to an enum column; the actual query generated may be different depending
638
+ # on the database adapter and the column definition.
639
+ #
640
+ # class Conversation < ActiveRecord::Base
641
+ # enum :status, [ :active, :archived ]
642
+ # end
643
+ #
644
+ # Conversation.in_order_of(:status, [:archived, :active])
645
+ # # SELECT "conversations".* FROM "conversations"
646
+ # # WHERE "conversations"."status" IN (1, 0)
647
+ # # ORDER BY CASE
648
+ # # WHEN "conversations"."status" = 1 THEN 1
649
+ # # WHEN "conversations"."status" = 0 THEN 2
650
+ # # END ASC
651
+ #
652
+ # +values+ can also include +nil+.
653
+ #
654
+ # Conversation.in_order_of(:status, [nil, :archived, :active])
655
+ # # SELECT "conversations".* FROM "conversations"
656
+ # # WHERE ("conversations"."status" IN (1, 0) OR "conversations"."status" IS NULL)
657
+ # # ORDER BY CASE
658
+ # # WHEN "conversations"."status" IS NULL THEN 1
659
+ # # WHEN "conversations"."status" = 1 THEN 2
660
+ # # WHEN "conversations"."status" = 0 THEN 3
661
+ # # END ASC
662
+ #
636
663
def in_order_of ( column , values )
637
664
klass . disallow_raw_sql! ( [ column ] , permit : model . adapter_class . column_name_with_order_matcher )
638
665
return spawn . none! if values . empty?
0 commit comments