DEV Community

Cover image for Rails: Add a column to a table
Rixio Barrios
Rixio Barrios

Posted on

Rails: Add a column to a table

To add a column to an existing table on Ruby on Rails you would need to create a rails command to generate that migration. Also, there will be an opportunity to name your migration under the context of what you need.

rails generate migration AddImageColumnToListings image:string
Enter fullscreen mode Exit fullscreen mode

This commands adds the image column to your listings table with the string content. A file would be added to your migrate folder.

class AddImageColumnToListings < ActiveRecord::Migration[6.1]
  def change
    add_column :listings, :image, :string
  end
end
Enter fullscreen mode Exit fullscreen mode

At this point you can make any changes to the files before running

rails db:migrate

Top comments (0)