How to add a new column in an existing table in Rails 5?

17,622

Solution 1

You forgot to add datatype, below is the updated migration.

class ChangeJobsTable < ActiveRecord::Migration[5.0]
  def change
    add_column :jobs, :skills2, :string
  end
end

Solution 2

You indeed forgot the datatype. You can also do it via the console in the future:

rails g migration AddSkills2ToJobs skills2:string

Share:
17,622
Amrinder Singh
Author by

Amrinder Singh

Updated on June 14, 2022

Comments

  • Amrinder Singh
    Amrinder Singh almost 2 years

    I want to add a new column in one of my table in Rails 5. I recently renamed a column by using the following way:

    rails g migration ChangeJobsTable
    

    then in 20160802104312_change_jobs_table.rb:

    class ChangeJobsTable < ActiveRecord::Migration[5.0]
      def change
        rename_column :jobs, :skills, :skills1
      end
    end
    

    then

    rails db:migrate
    

    It worked fine, but now if I want to also add a new column skills2, do I need to do it like this?

    class ChangeJobsTable < ActiveRecord::Migration[5.0]
      def change
        add_column :jobs, :skills2
      end
    end