[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 @   Zhentiw  阅读(481)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示