Comments in Rails migrations

Josua Schmid
1 min readJun 30, 2020

When (not “if”) your database design gets messy, other developers have a hard time to understand methods like this:

def full_address
"#{address} #{street_number}".strip
end

In the case of a Rails project I’d go to db/schema.rb to understand a bit better what’s going on. I’d be really glad if someone was thoughtful enough to put a comment there. While you cannot add Ruby comments directly to db/schema.rb without them being wiped each time the file is automatically regenerated, it is possible to add database comments. Since Rails 5, you can do this also via a migration helper method called change_column_comment:

class AddCommentAboutAddress < ActiveRecord::Migration[6.0]
def change
change_column_comment(:users, :address,
from: '', to: 'meaning "street"')
change_column_comment(:liable_people, :address,
from: '', to: 'meaning "street"')
end
end

This generated the following changes in db/schema.rb and the searcher for truth can at least understand what’s going on here.

--

--