Skip to content

Commit c06888d

Browse files
authored
Merge pull request rails#50826 from hexdevs/ta/document-in-order-of-with-enum
[docs] Add more examples to `#in_order_of` [ci-skip]
2 parents d1236c4 + 4335bc9 commit c06888d

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ def order!(*args) # :nodoc:
622622
self
623623
end
624624

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+.
626627
#
627628
# User.in_order_of(:id, [1, 5, 3])
628629
# # SELECT "users".* FROM "users"
@@ -633,6 +634,32 @@ def order!(*args) # :nodoc:
633634
# # WHEN "users"."id" = 3 THEN 3
634635
# # END ASC
635636
#
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+
#
636663
def in_order_of(column, values)
637664
klass.disallow_raw_sql!([column], permit: model.adapter_class.column_name_with_order_matcher)
638665
return spawn.none! if values.empty?

0 commit comments

Comments
 (0)