0

I have a Ruby on Rails application backed by a MySQL database. I want to add a new column of the MySQL type tinyint to one of my existing database tables.

After creating a new ActiveRecord migration class (via the command-line generator rails generate migration), what's the syntax to use in my add_column method call in my change method to add the new tinyint-type column? That is:

class MyMigration < ActiveRecord::Migration
  def change
    add_column :my_existing_table_name, :my_new_column_name,  # Q: What goes here?
  end
end
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • What you answered is correct, but also take a look at [this](https://stackoverflow.com/a/57485464/3935428) in case you still need AR to return this value as an integer and not a boolean – Harry Geo Sep 14 '20 at 15:41

1 Answers1

0

Just use a value of :boolean for the type parameter in the add_column call. For example:

class MyMigration < ActiveRecord::Migration
  def change
    add_column :my_existing_table_name, :my_new_column_name, :boolean
  end
end

Reference: https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_column

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170