Skip to main content

Adding, deleting, and changing tables in Ruby

Ruby table additions, deletions, and modifications.

Official Link: Active Record Migrations - Ruby on Rails Guides creating-a-standalone-migration "Active Record Migrations - Ruby on Rails Guides")

Add the string field

    rails g migration AddFieldIdToTables field_id:string

Add boolean(tinyint) field

    rails g migration AddFieldFlagToTables field_flag:boolean

Add text field

    rails g migration AddFieldJsonToTables field_json:text

Add Integer field

    rails g migration AddFieldDaysToTables field_days:integer

Delete string field

    rails g migration RemoveEmailFromTables email:string

Rename the Text field to Integer (not recommended to change the data type, it is time consuming and it is recommended to delete it and create a new one)

    rails g migration RenameFieldFromTables
    
2. def change

3. rename_column :tables, :field, :field_id

4. change_column :tables, :field_id, :integer

5. end

6. end



Don't forget to run it when you're done:

    rake db:migrate
``