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
  end
end

......................................................................................................................................................................
......................................................................................................................................................................



Note  02    :: :::::::::       :: :::::::::      :: :::::::::         :: :::::::::        :: :::::::::       :: :::::::::      :: :::::::::

How to Remove column in Rails:

$ rails generate migration RemovePartNumberFromProducts part_number:string

class RemovePartNumberFromProducts < ActiveRecord::Migration[5.0]
  def change
    remove_column :products, :part_number, :string
  end
end



......................................................................................................................................................................
......................................................................................................................................................................





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
  end
end

......................................................................................................................................................................
......................................................................................................................................................................



.......................................................                                                  .............................................................

.......................................................................................................................................................................

                                     TYPE B  ::::::::::::::::::::::::::::::::  Model Generators

                                                           Note:  Make Tables

..................................................................................................................................................................

.......................................................................................................................................................................

Note  01    :: :::::::::       :: :::::::::      :: :::::::::         :: :::::::::        :: :::::::::       :: :::::::::      :: :::::::::

How to Generate  Tables in Rails:

rails generate model Product name:string description:text


class CreateProducts < ActiveRecord::Migration[5.0]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description
 
      t.timestamps
    end
  end
end



......................................................................................................................................................................
......................................................................................................................................................................











Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5