Junior Track Roadmap

ActiveRecord querying: where/find_by

This course covers filtering records in ActiveRecord with .where and find_by. It starts with a multi-key hash, where every condition is joined with AND. Ranges generate BETWEEN and arrays generate IN. .where.not negates a condition, and .or joins two relations with OR. Chained .where calls produce the same SQL as one hash, and the relation stays lazy. find_by accepts several attributes and returns one match or nil, while find_by! raises ActiveRecord::RecordNotFound. A closing challenge rebuilds the AND matching in plain Ruby.

What you'll learn

  • ✓Combining several conditions in one .where hash with AND
  • ✓Using ranges and arrays to generate BETWEEN and IN
  • ✓Negating conditions with .where.not and joining relations with .or
  • ✓Chaining .where calls to build a lazy query across several lines
  • ✓Looking up one record with find_by and several attributes
  • ✓Raising ActiveRecord::RecordNotFound with find_by! when nothing matches
Free preview · Lesson 1 2 min read

.where with several conditions at once

The lesson "find_by: the same idea, without the crash" in ActiveRecord: finding records showed the basic shape. Article.find_by(title: "Nonexistent") looks up one attribute and returns nil when nothing matches. That lesson left the rest for a later course: find_by with several conditions, chaining…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...