Back to Course

Reading the Source · Card 9

What does enum generate?

A one-line declaration that adds predicates, scopes, bang-setters, and a typed casting layer over an integer column. Knowing the surface helps you read the model.

The familiar line

A Post model with three lifecycle states.

class Post < ApplicationRecord
  enum status: { draft: 0, published: 1, archived: 2 }
end

The question

List the methods enum adds to a Post instance and to the Post class.

Take a moment. One predicate per value. One setter per value. One scope per value. Plus a couple of class-level helpers and a casting layer that lets you write status as a string or symbol even though the column is an integer.