13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails]ModelやControllerの削除

Posted at

##Modelの削除
まずはModelを作成

$ rails g model hoge name:string address:string
      invoke  active_record
      create    db/migrate/20200122093003_create_hoges.rb
      create    app/models/hoge.rb
      invoke    test_unit
      create      test/models/hoge_test.rb
      create      test/fixtures/hoges.yml

作成したモデルを削除

$ rails destroy model hoge
      invoke  active_record
      remove    db/migrate/20200122093003_create_hoges.rb
      remove    app/models/hoge.rb
      invoke    test_unit
      remove      test/models/hoge_test.rb
      remove      test/fixtures/hoges.yml

##Controllerの削除
まずはコントローラーを作成

$ rails g controller foobar
      create  app/controllers/foobar_controller.rb
      invoke  erb
      create    app/views/foobar
      invoke  test_unit
      create    test/controllers/foobar_controller_test.rb
      invoke  helper
      create    app/helpers/foobar_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/foobar.coffee
      invoke    scss
      create      app/assets/stylesheets/foobar.scss

作成したコントローラーを削除

rails destroy controller foobar
      remove  app/controllers/foobar_controller.rb
      invoke  erb
      remove    app/views/foobar
      invoke  test_unit
      remove    test/controllers/foobar_controller_test.rb
      invoke  helper
      remove    app/helpers/foobar_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      remove      app/assets/javascripts/foobar.coffee
      invoke    scss
      remove      app/assets/stylesheets/foobar.scss

##scaffoldで一括作成したものを削除
まずはscaffoldで作成

$ rails g scaffold Hoge name:string email:string                        
      invoke  active_record
      create    db/migrate/20200122093732_create_hoges.rb
      create    app/models/hoge.rb
      invoke    test_unit
      create      test/models/hoge_test.rb
      create      test/fixtures/hoges.yml
      invoke  resource_route
       route    resources :hoges
      invoke  scaffold_controller
      create    app/controllers/hoges_controller.rb
      invoke    erb
      create      app/views/hoges
      create      app/views/hoges/index.html.erb
      create      app/views/hoges/edit.html.erb
      create      app/views/hoges/show.html.erb
      create      app/views/hoges/new.html.erb
      create      app/views/hoges/_form.html.erb
      invoke    test_unit
      create      test/controllers/hoges_controller_test.rb
      invoke    helper
      create      app/helpers/hoges_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/hoges/index.json.jbuilder
      create      app/views/hoges/show.json.jbuilder
      create      app/views/hoges/_hoge.json.jbuilder
      invoke  test_unit
      create    test/system/hoges_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/hoges.coffee
      invoke    scss
      create      app/assets/stylesheets/hoges.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

scaffoldによって一括作成されたものを削除する

$ rails destroy scaffold hoges                                          
      invoke  active_record
      remove    db/migrate/20200122093732_create_hoges.rb
      remove    app/models/hoge.rb
      invoke    test_unit
      remove      test/models/hoge_test.rb
      remove      test/fixtures/hoges.yml
      invoke  resource_route
       route    resources :hoges
      invoke  scaffold_controller
      remove    app/controllers/hoges_controller.rb
      invoke    erb
      remove      app/views/hoges
      remove      app/views/hoges/index.html.erb
      remove      app/views/hoges/edit.html.erb
      remove      app/views/hoges/show.html.erb
      remove      app/views/hoges/new.html.erb
      remove      app/views/hoges/_form.html.erb
      invoke    test_unit
      remove      test/controllers/hoges_controller_test.rb
      invoke    helper
      remove      app/helpers/hoges_helper.rb
      invoke      test_unit
      invoke    jbuilder
      remove      app/views/hoges
      remove      app/views/hoges/index.json.jbuilder
      remove      app/views/hoges/show.json.jbuilder
      remove      app/views/hoges/_hoge.json.jbuilder
      invoke  test_unit
      remove    test/system/hoges_test.rb
      invoke  assets
      invoke    coffee
      remove      app/assets/javascripts/hoges.coffee
      invoke    scss
      remove      app/assets/stylesheets/hoges.scss
      invoke  scss

既にmigrateしてある場合は、データベースにテーブルが残ってしまうので、下記の手順も必要。
まずはマイグレーションファイルを作成する。

$ rails g migration hoges

作成したマイグレーションファイルにhogesテーブルの削除命令を書く。

class Hoges < ActiveRecord::Migration[5.1]
  def change
    drop_table :hoges
  end
end

あとはmigrateして完了

$ rails db:migrate
13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?