Junior Track Roadmap

ActiveModel: validations & naming

This course covers two parts of ActiveModel. The first is naming: model_name returns an ActiveModel::Name object with nine naming values plus human, used by partial lookup, form field names, route helpers and I18n. A namespace changes element, param_key, route_key and i18n_key in different ways. Overriding self.model_name lets a form object borrow another model's identity. The second part is validation: EachValidator classes for reusable per-attribute checks, and ActiveModel::Validator with validates_with for checks on the whole record.

What you'll learn

  • ✓What each field of ActiveModel::Name holds and where Rails reads it
  • ✓How a namespace changes element, param_key, route_key and i18n_key
  • ✓Overriding self.model_name to borrow another model's naming, and the confusion it can cause
  • ✓How human looks up a translation and falls back to a humanized name
  • ✓Writing a reusable EachValidator class that respects allow_nil and allow_blank
  • ✓Whole-record checks with ActiveModel::Validator and validates_with
Free preview · Lesson 1 3 min read

ActiveModel::Naming: what model_name returns

"Form objects: complex logic" covered ActiveModel::Model, ActiveModel::Attributes, and how form_with calls model_name, persisted? and to_key on a plain Ruby object to build a working form. That course treated model_name as one method returning one thing Rails needs. This course picks up there.…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...