Junior Track Roadmap

ActiveRecord: enums

This course covers Rails enums through the code of one app. It starts with an integer level column and the enum line that maps each stored number to a name. Next come the ? and ! methods on instances. Then come the class scopes and the plural mapping hash, which reads in both directions. The course shows why assignment raises on a bad value while .where quietly matches nothing. It also covers what enum validates by default, the validate option, and sharing one mapping across models.

What you'll learn

  • ✓How an enum declaration maps integer column values to names
  • ✓The ? and ! instance methods and the class scopes enum generates
  • ✓Reading the plural mapping hash from name to integer and back
  • ✓Why assignment raises on an invalid value while .where does not
  • ✓What enum validates on its own and how the validate option changes it
  • ✓Reusing one mapping across models with prefix, and why to use explicit hashes
Free preview · Lesson 1 2 min read

enum: turning a bare integer into a name you can read

Catalog::Course has a level column. This app's migration, db/migrate/20260226000001_create_courses.rb, defines it:

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...