Rails for Zombies 2 Level 1 - mikesabat/LR_HF GitHub Wiki
-2. Installing - railstutorial.org will help you install rails and get an application installed on Heroku.
-3. Creating a Rails app -- "rails new AppName". This command will create the directories for your rails app. The last line will reference "bundle install". Bundle install goes to the internet and installs of the apps dependencies -> gems.
-4. Rails commands. If you CD into the app directory, you can use rails commands. Generate (g), Server (s), Console (c), Database Console (db). If you add -h it gives you options, such as 'rails server -h'.
-7. Generate (g). You can generate scaffold, migration, helper, mailer, model. Generating scaffold syntax - rails g scaffold NAME field:type field:type.
Types refer to database types: string, text, integer, boolean, decimal, float, binary, date, time, datetime
Scaffolding creates several files.
Model: Invokes the ActiveRecord, creates the Model and the Migration
Routes: Creates Resources :AppName
Controller: Creates the Controller
Views: Creates New, Show, Edit, Index
-8. Migrations - How we make changes to the database from inside rails. Rails automatically creates the table with the primary key "Id". Rails also adds a timestamp to the migration which tracks when the table was created or updated. Timestamp is magic and rails automatically updates it.
-9. Need to run the migration file before you start the server. rake db:migrate
-10. Rails Console (c) - you can start the rails console and start running code model.Create(stock: "ABC", date: 1-29-78). Remember with Ruby, everything returns a value.
-11. Hash syntax for Rails 1.9 The old syntax { :name => "Michael" } new syntax { name: "Michael" }
-12. Writing Migrations - "rails g migration AddTo field:type"
-For example "rails g migration AddChangeToStocks change:integer" or "rails g migration AddChangeAndDelayToStocks change:integer delay:time"
The Migration File would look like:
class AddChangeAndDelayToStocks <ActiveRecord: :Migration
__def change
____add_column :stocks, :change, :integer, default: false (or 0)
____add_colums :stocks, :delay, :time, [migration options, like default]
__end
end
Migration options -- default: , limit: 30, null: true/false, first: true (position) after: :email (:email is a column, position), unique: true
-13. rake commands
rake db:migrate - this will run all the current migrations
rake db:rollback - rollback the current migrations = changes to the database
rake db:schema:dump - this is run when migrate happens. It summarizes the current database schema
rake db:setup - this creates the database and loads the schema
-14. More migration commands - rails g migration RemoveColumnFromTable
class RemoveColumnFromTable <ActiveRecord: :Migration __def up
____remove_column :table, :column
__end
__def down
____add_column :table, :column, :type
__end
Other Migration Commands:
- rename_column
- rename_table
- drop_table
- change_column
- change_column_default
More information on migrations is available at guides.rubyonrails.org