Junior Track Roadmap

ActiveRecord querying: order/limit

This course covers how ActiveRecord sorts and restricts query results, using a six-row ScratchArticleQ table and real queries from this app. It explains why rows have no guaranteed order without .order, why ascending is the default and how :desc flips it, and how several columns break ties. It then covers .limit and .offset for pagination, the SQL behind .first(n) and .last(n), and how .reorder replaces an existing order. A final query object combines these methods to paginate a comment thread.

What you'll learn

  • ✓Why query results have no guaranteed order without .order
  • ✓Sorting ascending by default and descending with a hash like views: :desc
  • ✓Ordering by several columns so later columns break ties
  • ✓Paginating with .limit and .offset after an explicit order
  • ✓How .first(n) and .last(n) build their SQL
  • ✓Replacing an existing order with .reorder instead of appending
Free preview · Lesson 1 3 min read

.order: ascending unless you say otherwise

ActiveRecord querying: where/find_by spent seven lessons on one question: which rows come back. .where and find_by narrow a table to the rows that match a condition, and that course ran every example on the scratch ScratchArticleQ table (title, category, published, views). This course asks a…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...