Rails ::::::::::: Formulas and structures
Rails :: ::::::::: Formulas and structures
....................................................... .............................................................
.......................................................................................................................................................................
TYPE A :::::::::::::::::::::::::::::::: Add Columns and Remove Columns
Note: After Every Command Run rake db:migrate or rails db:migrate
..................................................................................................................................................................
.......................................................................................................................................................................
Note 01 :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: :::::::::
How to Add column in Rails:
$ rails generate migration AddPartNumberToProducts part_number:string |
$ rails generate migration AddPartNumberToProducts part_number:string:index |
Will Generate
class AddPartNumberToProducts < ActiveRecord::Migration[5.0] def change add_column :products, :part_number, :string add_index :products, :part_number endend |
Note 02 :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: :::::::::
$ rails generate migration RemovePartNumberFromProducts part_number:string |
class RemovePartNumberFromProducts < ActiveRecord::Migration[5.0] def change remove_column :products, :part_number, :string endend |
Note 03 :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: :::::::::
How to Add Multiple columns in Rails:
$ rails generate migration AddDetailsToProducts part_number:string price:decimal |
class AddDetailsToProducts < ActiveRecord::Migration[5.0] def change add_column :products, :part_number, :string add_column :products, :price, :decimal endend |
....................................................... .............................................................
.......................................................................................................................................................................
TYPE B :::::::::::::::::::::::::::::::: Model Generators
Note: Make Tables
..................................................................................................................................................................
.......................................................................................................................................................................
Note 01 :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: ::::::::: :: :::::::::
How to Generate Tables in Rails:
class CreateProducts < ActiveRecord::Migration[5.0] def change create_table :products do |t| t.string :name t.text :description t.timestamps end endend |
Comments
Post a Comment