Add multiple columns to an existing table with a Rails migration

/ tips-and-tricks / ruby ,  ruby on rails

There are plenty of shortcuts with Ruby on Rail's implementation of migrations. Adding columns, removing column, add indexes, altering table or columns to just name a few. I recently ran across a simple trick that can be utilized to add multiple columns quickly to a migration

By utilizing the following formula with the Rails generator, you can add multiple columns to a single migration.

rails g migration add_first_column_and_second_column_to_model first_column:string second_column:string

The Rails generate command is smart enough to figure out the correct database table and construct a migration based off it. I imagine this could also be used with other migrations, not just altering an existing table.

Got a Rails generate trick or tip? Tell me about it below in the comments.

Join the conversation

comments powered by Disqus