Columns
Column Types
- :primary_key
- :string: short-form text
- :text: long-form text
- :integer: whole numbers
- :float: double-precision floating-point numbers [3.141]
- :decimal: high-precision floating-point numbers [3.1415926535 8979323846 26433832]
- :boolean (true or false)
- :binary: (1 or 0)
- :date
- :time
- :datetime
Add a Column
bash
bin/rails g migration AddKeywordsSizeToProduct keywords:string size:string
Migration for Adding a Column
ruby
class AddKeywordsSizeToProduct < ActiveRecord::Migration[6.1]
def change
add_column :products, :keywords, :string
add_column :products, :size, :string
end
end
Remove A Column
bash
bin/rails g migration RemoveKeywordsFromProduct keywords
Migration for Removing a Column
ruby
class RemoveKeywordsFromProduct < ActiveRecord::Migration[6.1]
def change
remove_column :products, :keywords, :string
end
end
Rename a Column
bash
bin/rails g migration RenameProductPopularityToRanking
Migration for Renaming a Column
ruby
class RenameProductPopularityToRanking < ActiveRecord::Migration[6.1]
def change
rename_column :products, :popularity, :ranking
end
end
Change a Column Type
bash
bin/rails g migration ChangeProductPopularity
Migration for Changing Column Type
ruby
class ChangeProductPopularity < ActiveRecord::Migration[6.1]
def change
change_column :products, :ranking, :decimal, precision: 10, scale: 2
end
end