[Rails Level 2] From Ground up -- Ex

RAILS NEW

Enter the command to start a new rails app called 'ZombieTweets'.

rails new ZombieTweets

 

GENERATE SCAFFOLD

Enter the command to create tweet scaffold with a string status and an integer of zombie_id.

rails g scaffold tweet status:string zombie_id:integer

basicllyt, it will create db, controller, views and model.

invoke  active_record
create    db/migrate/20140919131116_create_tweets.rb
create    app/models/tweet.rb
invoke    test_unit
create      test/unit/tweet_test.rb
create      test/fixtures/tweets.yml
route  resources :tweets
invoke  scaffold_controller
create    app/controllers/tweets_controller.rb
invoke    erb
create      app/views/tweets
create      app/views/tweets/index.html.erb
create      app/views/tweets/edit.html.erb
create      app/views/tweets/show.html.erb
create      app/views/tweets/new.html.erb
create      app/views/tweets/_form.html.erb
invoke    test_unit
create      test/functional/tweets_controller_test.rb
invoke    helper
create      app/helpers/tweets_helper.rb
invoke      test_unit
create        test/unit/helpers/tweets_helper_test.rb
invoke  assets
create    app/assets/javascripts/tweets.js.coffee
invoke    scss
create      app/assets/stylesheets/tweets.css.scss
invoke  scss
create    app/assets/stylesheets/scaffolds.css.scss

 

CREATE TWEETS TABLE

Write the migration manually which creates the tweets table in the database with the status string column and zombie_id integer column.

class CreateTweets < ActiveRecord::Migration
  def change
    create_table :tweets do |t|
      t.string :status
      t.integer :zombie_id
    end
  end
end

 

RUN MIGRATION

Enter the command to run the migration you just created.

rake db:mi­grate

 

BOOT IT UP

Enter the command to start a rails server.

rails server / rails s

 

ADD COLUMN TO TABLE

Enter the command line text to generate a migration called AddPrivacyToTweets which adds a boolean field called private.

rails g migra­tion AddPr­ivacyToTwe­ets priva­te:boolean

#Add<column>To<table>

 

CREATE MIGRATION BY HAND

Create migration by hand that adds two fields to the tweets table: a location string field which has alimit of 30 and a boolean field called show_location which defaults to false.

class AddLocationToTweets < ActiveRecord::Migration
  def change
    add_column :tweets, :location, :string, limit:30
    add_column :tweets, :show_location, :boolean, default: false
  end
end

 

ROLLBACK MIGRATION

Assume we ran that last migration. However we forgot a column we wanted to add, could you please roll it back from the command line?

rake db:rollback

 

CHANGE TABLE

Now that we've rolled back, add a category_name string field and use the rename command to rename the status column to message instead.

class AddLocationToTweets < ActiveRecord::Migration
  def change
    add_column :tweets, :location, :string, limit: 30
    add_column :tweets, :show_location, :boolean, default: false
  end 
end

Answer:

class AddLocationToTweets < ActiveRecord::Migration
  def change
    add_column :tweets, :location, :string, limit: 30
    add_column :tweets, :show_location, :boolean, default: false
    add_column :tweets, :category_name, :string
    rename_column :tweets, :status, :message
  end 
end

 

REMOVE COLUMN

On second thought, that category_name string column was a bad idea. Write a migration to remove thecategory_name column.

class RemoveCategoryNameFromTweets < ActiveRecord::Migration
  def up
    remove_column :tweets, :category_name
  end

  def down
    add_column :tweets, :category_name, :string
  end
end

 

DATABASE SETUP

You've decided to install the app on another computer. Please enter the command you should use (instead of rake db:migrate) to create the database, load the schema, and run the seed file.

rake db:setup

 

IN THE CONSOLE

Enter the command to enter the Rails console.

rails console

 

posted @ 2014-09-20 01:40  Zhentiw  阅读(486)  评论(0)    收藏  举报