---
title: Add comments to your tables columns
description: Add extra documentation to your columns attributes with this one weird
  trick.
author:
  name: Rémi Mercier
  url: https://remimercier.com/about/
canonical_url: https://remimercier.com/comment-tables-columns-attributes/
published: '2024-06-22'
language: en
topics:
- Rails
site:
  name: Rémi Mercier
  url: https://remimercier.com
---

# Add comments to your tables columns

> Add extra documentation to your columns attributes with this one weird trick.

Published on 2024-06-22 by Rémi Mercier.
Original: https://remimercier.com/comment-tables-columns-attributes/

I've been looking for a way to add default comments on models attributes *for ages*!

By default, Rails adds comments to some attributes like `id` (`not null, primary key` for instance.)

But now, I can add my own. For example, I can specify the unit for a duration column. It's like Yard documentation for methods, but for attributes.

These default comments are defined in your `schema.rb`, so gems like [annotate](https://github.com/ctran/annotate_models){:target="\_blank"} will pick them up. No need to worry about overwrite anymore.

```ruby
  class Book
    # == Schema Information
    #
    # Table name: books
    #
    #  id          :integer    👉 not null, primary key 👈
    #  duration    :integer   👉 in seconds 👈
    #
  end
```

See this lil' `in seconds` above, that's me, adding extra documentation to my tables column so my teamates don't have to guess the unit.

Here's how to do that in a migration adding a new column:

```ruby
class AddDurationToBooks < ActiveRecord::Migration[7.0]
  def change
    add_column :books, :duration, :integer, comment: "in seconds", default: 0, null: false
  end
end
```

I can also add a default comment to an existing attribute:

```ruby
class AddCommentToBooksDuration < ActiveRecord::Migration[7.0]
  def change
    change_column_comment(:books, :duration, from: nil, to: "in seconds")
  end
end
```

Passing a hash containing `:from` and `:to` will make this change reversible in migrations.

Go and document those attributes!

Cheers,

Rémi - [@remi@ruby.social](https://ruby.social/@remi)

---

Written by Rémi Mercier (https://remimercier.com/about/), independent Ruby on Rails developer and technical writer.
Original article: https://remimercier.com/comment-tables-columns-attributes/
When quoting or referencing this article, please cite "Add comments to your tables columns" by Rémi Mercier, https://remimercier.com/comment-tables-columns-attributes/
