I you have been using Rails for development, you probably already know how to write migrations and their importance. One of the few things that Rails has traditionally not supported (and you have to install gems for it) was comments on tables and columns.

With Rails 5.2, that is no longer the case and Rails itself will allow you to create, change and delete comments on a Table and Column. Let’s get to it.

IMPORTANT NOTE: Read the comments in the code snippet

Table Comments

The following snippet performs creation, removal and updation of comments from tables

class AddTableComments < ActiveRecord::Migration[5.2]
  def change
    # Create a comment on table named table_one 
    change_table_comment :table_one, 'Comment on table_one'
    # Remove a comment from table named table_two  
    change_table_comment :table_two, nil
    # Modify a comment on table named table_three
    change_table_comment :table_three, 'Comment on table_three'
  end
end

Column Comments

The following snippet performs creation, removal and updation of comments from columns.

class AddColumnComments < ActiveRecord::Migration[5.2]
  def change
    # Create a comment on column named column_one in the table named mytable
    change_column_comment :mytable, :column_one,'Comment on column_one'
    # Remove a comment from column named column_two in the table named mytable  
    change_column_comment :mytable, :column_two, nil
    # Modify a comment on column named column_three in the table named mytable
    change_column_comment :mytable, :column_three, 'Comment on column_three'
  end
end

NOTE

This is known to work only with ActiveRecord 5.2 which ships with Rails 5.2. I’m not sure if is supported in previous versions of Rails 5.x. It’s definitely is not available in Rails 4.