Skip to content

Commit 3773dc8

Browse files
committed
Add order arguments section
1 parent b077bc2 commit 3773dc8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

README.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,21 @@ scope :chronological, -> { order(id: :asc) }
967967
scope :chronological, -> { order(created_at: :asc) }
968968
----
969969

970+
=== Order arguments [[order-arguments]]
971+
972+
Prefer symbol arguments over strings for ordering.
973+
974+
String columns without a table name can cause an "ambiguous column name" error when ordering by a column that exists in multiple tables joined in the same query.
975+
976+
[source,ruby]
977+
----
978+
# bad
979+
User.order('created_at DESC')
980+
981+
# good
982+
User.order(created_at: :desc)
983+
----
984+
970985
=== `pluck`
971986

972987
Use https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck[pluck] to select a single value from multiple records.

0 commit comments

Comments
 (0)