Order By Before Where (ARCH6076)
Ce contenu n’est pas encore disponible dans votre langue.
Key ruby.performance.orderby-before-where · Severity warning · Category performance
Sorting before filtering in a collection chain sorts a larger set than needed. Filter first, then sort.
Incorrect
Section titled “Incorrect”def top_active(users) users.sort_by(&:created_at) .select(&:active?) .first(10)endCorrect
Section titled “Correct”def top_active(users) users.select(&:active?) .sort_by(&:created_at) .first(10)endApply the filter before the sort in the chain so a smaller set is sorted.