skip to content

In Rails 8.1, what does bin/rails generate model Kit name:string! 'price:decimal{10,2}' supplier:references sku:string:uniq write into the migration and model?

level: middleimportance: must knowfreq 50%

answer

  1. trailing ! means null: false
  2. {10,2} sets precision and scale
  3. quote braces against shell expansion
  4. references: belongs_to, null: false, foreign_key
  5. :uniq adds add_index unique: true

basics

~20 s

The migration gets name with null: false, price as decimal precision 10 scale 2, a supplier reference with null: false and foreign_key: true, and a unique index on sku; the model gets only belongs_to :supplier, and nothing validates name or sku.

solid answer

~40 s

Each argument is `name:type{options}:index`. A trailing `!` on the type adds `null: false`. `decimal{10,2}` becomes `precision: 10, scale: 2`; the quotes stop the shell's brace expansion from splitting it into two words (`decimal{10.2}` or `decimal{10-2}` also parse). `supplier:references` becomes `t.references :supplier, null: false, foreign_key: true` (indexed by default, `null: false` because `belongs_to` is required by default) and `belongs_to :supplier` in the model. `:uniq` adds `add_index :kits, :sku, unique: true` after the table, `:index` a plain one, and a missing type means `string`. Generators write database constraints, not model validations: nothing adds `validates :name, presence: true`. Special types `password:digest`, `token`, `attachment`, `attachments` and `rich_text` also write model macros.

code

bash · 1 line
bash
bin/rails generate model Kit name:string! 'price:decimal{10,2}' supplier:references sku:string:uniq

go deeper

for a junior

Know the name:type form, that a missing type means string, and that references writes belongs_to plus a supplier_id column.

for a middle

Explain !, {10,2}, :index and :uniq precisely, the shell quoting trap, and that references comes with null: false, an index and a foreign key.

for a senior

Insist the database constraints match the model: add validations next to ! and :uniq, check reverse associations, and review the migration before it runs on a large table.

for a principal

Treat generator syntax as a team convention: decide which constraints every new table must carry and encode that in templates or review checklists, not memory.

## The grammar of a generator attribute Model, resource and scaffold generators take attributes in the form `name[:type[{options}][!]][:index|:uniq]`. Rails' `GeneratedAttribute.parse` splits each argument on `:` and reads it piece by piece: | Piece | Example | Effect | |---|---|---| | name only | `title` | a `string` column | | type | `price:decimal` | the column type | | `{n}` | `name:string{80}` | `limit: 80` for string, text, binary, integer | | `{p,s}` | `'price:decimal{10,2}'` | `precision: 10, scale: 2` | | `{polymorphic}` | `owner:references{polymorphic}` | adds `owner_type` and `polymorphic: true` | | trailing `!` | `name:string!` | `null: false` | | `:index` | `sku:string:index` | a plain index | | `:uniq` | `sku:string:uniq` | a unique index | | name with no type, then index | `sku:uniq` | a `string` column with a unique index | Unknown types raise `Could not generate field 'x' with unknown type 'y'`, and a name Active Record already defines as a method (such as `save` or `errors`) raises before anything is written. ## The meal-kit command, line by line `bin/rails generate model Kit name:string! 'price:decimal{10,2}' supplier:references sku:string:uniq` writes the migration shown in the code example and this model: ```ruby class Kit < ApplicationRecord belongs_to :supplier end ``` - **`name:string!`** produces `t.string :name, null: false`, a database `NOT NULL` constraint. - **`'price:decimal{10,2}'`** produces `t.decimal :price, precision: 10, scale: 2`. The quotes matter: bash and zsh treat `{10,2}` as **brace expansion** and would pass `price:decimal10 price:decimal2`, which fails as an unknown type. The parser also accepts `.` or `-` between the numbers, so `price:decimal{10.2}` needs no quotes. - **`supplier:references`** produces `t.references :supplier, null: false, foreign_key: true`. `t.references` adds an index on `supplier_id` by default; `null: false` comes from `belongs_to_required_by_default`, which `load_defaults` has set to true for years; `foreign_key: true` is added for every non-polymorphic reference. The model gets `belongs_to :supplier`. - **`sku:string:uniq`** produces `t.string :sku` inside `create_table` and `add_index :kits, :sku, unique: true` after it. For a reference, `:uniq` instead becomes an inline `index: { unique: true }`. - `t.timestamps` is added unless you pass `--no-timestamps`. ## Types that also touch the model Some types are shortcuts for a column plus a macro, or for a macro alone: 1. `password:digest` writes a `password_digest` string column and `has_secure_password`. 2. `auth_token:token` writes a string column, a unique index and `has_secure_token :auth_token`. 3. `photo:attachment` and `photos:attachments` write **no column**; the model gets `has_one_attached` or `has_many_attached`. 4. `description:rich_text` writes no column and adds `has_rich_text :description`. ## What the syntax does not do - **No validations for those columns.** `!` and `:uniq` are database constraints. The only validation in the model is the one `belongs_to :supplier` brings (the supplier must exist), so `Kit.new(name: nil, supplier: supplier).valid?` returns true until you add `validates`, and a duplicate SKU surfaces as `ActiveRecord::RecordNotUnique` from the database. - **No defaults.** There is no `{default: ...}` shortcut; edit the migration for that. - **No associations on the other side.** `belongs_to :supplier` is written on `Kit`; `has_many :kits` on `Supplier` is yours to add. - **No running.** The migration is a file; `bin/rails db:migrate` applies it. Reading the attribute list back is a quick review: if the command line does not say `!` or `:uniq`, the database will not enforce it. ## Combining the pieces The pieces compose left to right, and the USAGE text allows one brace option with an index suffix: - `username:string{30}:uniq` writes `t.string :username, limit: 30` and a unique index; - `'price:decimal{10,2}!'` writes precision, scale and `null: false` together; - `owner:references{polymorphic}:index` keeps the polymorphic reference and its index; - `supplier:references:uniq` writes `index: { unique: true }` on the reference, a one-kit-per-supplier rule. The same syntax works for `resource` and `scaffold`, where it also drives the form fields: `price` gets a `text_field`, `supplier_id` a plain `text_field` too (a select is yours to write), and a boolean a `checkbox`.

  • Why does bin/rails generate model Kit price:decimal{10,2} without quotes fail in bash?
    Bash and zsh brace-expand `price:decimal{10,2}` into two words, `price:decimal10` and `price:decimal2`, before Rails sees them. Rails then rejects `decimal10` as an unknown type. Quote the argument, or write `price:decimal{10.2}` or `{10-2}`, which the parser also accepts and the shell leaves alone.
  • Does sku:string:uniq stop two kits with the same SKU from saving?
    It adds a unique index, so the database rejects the second row and Active Record raises `ActiveRecord::RecordNotUnique` from `save`. Nothing adds a uniqueness validation, so `valid?` still returns true for the duplicate and users see an exception rather than a form error. Add `validates :sku, uniqueness: true` for a friendly message and keep the index as the real guarantee.
  • What does supplier:references{polymorphic} write instead of a plain reference?
    It writes `t.references :supplier, polymorphic: true, null: false`, which creates `supplier_id` and `supplier_type` columns with an index across both, and `belongs_to :supplier, polymorphic: true` in the model. No `foreign_key: true` is added, because one column cannot reference several tables.

  • name:string! adds validates :name, presence: true to the model
  • sku:string:uniq adds a uniqueness validation to the model
  • supplier:references writes a plain integer column with no index
  • Unquoted price:decimal{10,2} works fine in bash
  • photo:attachment adds a photo string column to the table
  • The generator also adds has_many :kits to Supplier

Assumes Rails 8.1 generators, which accept the trailing ! for null: false alongside the {} options and :index/:uniq suffixes.