109
85

More than 1 year has passed since last update.

【Ruby on Rails】テーブル名の変更方法と変更時にやること一覧&ステップ

Last updated at Posted at 2019-10-31

テーブル名を変更する必要があったのですが、それに伴いいろいろな箇所を変更しなければならず混乱してしまいました。今後同じ作業をする必要がある方のために、まとめます。

やりたいこと

  • テーブル名を修正する
  • モデルやコントローラーの名前を修正する
  • テスト(今回はRspecを使用している場合)の対応も行う

環境

  • Rails 5

前提

今回は、テーブル名trashestrash_objectsに変更すると仮定して進めます。

方法

1. マイグレーションファイルを作成

rails g migrationコマンドを実行し、change_[old_table_name]_to_[new_table_name]といった名前のマイグレーションファイルを作成します。

terminal
$ rails g migration change_trashes_to_trash_objects

2. マイグレーションの中身を書いていく

マイグレーションファイルの中身を書いていきます。

20191031040820_change_trashes_to_trash_objects.rb
class ChangeTrashesToTrashObjects < ActiveRecord::Migration[5.2]
  def change
    rename_table :trashes, :trash_objects
  end
end

3. モデルを修正する

  1. Trashモデルのファイル名をtrash.rbからtrash_object.rbに変更します。ファイル名は単数形なので注意しましょう!
  2. trash_object.rbのクラス名をTrashからTrashObjectに変更します。
trash_object.rb
# 修正前
class Trash < ApplicationRecord
 belongs_to :address
end

# 修正後
class TrashObject < ApplicationRecord
  belongs_to :address
end
  1. Trashに関連していたアソシエーションを修正する

関連しているアソシエーションを書き換えていきます。

address.rb
# 修正前
class Address < ApplicationRecord
  belongs_to :trash
end

# 修正後
class Address < ApplicationRecord
  has_many :trash_objects
end

4.コントローラーを修正する

  1. Trashコントローラーのファイル名をtrashes_controller.rbからtrash_objects_controller.rbに変更します
  2. trash_objects_controller.rbのクラス名をTrashesControllerからTrashObjectsControllerに変更します
trash_objects_controller.rb
# 修正前
class TrashesController < ApplicationController
end

# 修正後
class TrashObjectsController < ApplicationController
end

3.メソッドを修正する
コントローラのメソッド内に存在している変数名やクラス名などを修正していきます。

trash_objects_controller.rb
# 修正前
 def index
  trashes = Trash.all
 end

# 修正後
 def index
  trash_objects = TrashObject.all
 end

5. ビューを修正する

viewsフォルダーの名前をtrashesからtrash_objectsに変更します。 viewフォルダー内で使われている変数名なども必要に応じて変更してください。

6. マイグレートを忘れずに!

このタイミングでmigrateをすると良いでしょう。
routes.rbを修正する前にdb:migrateをしないとエラーになるそうです!

terminal
rails db:migrate

7. ルートを修正する

今度はルートを修正していきます。

routes.rb
 # 修正前
 # get 'trashes/index', to: 'trashes#index'
 # get 'trashes/show', to: 'trashes#show'

# 修正後
 get 'trash_objects/index', to: 'trash_objects#index'
 get 'trash_objects/show', to: 'trash_objects#show'

8. テストコードの修正

  1. ファイル名をtrashes_spec.rbからtrash_objects_spec.rb修正します
  2. _pathを使っている場合は、新しい名前でのパス名に修正します
spec/trash_objects_spec.rb
# 修正前
# RSpec.describe 'TrashesAPI', type: :request do
#  describe 'GET /api/v1/trashes/index' do
#   it 'returns successfully' do
#     get api_v1_trashes_index_path

      # ....[省略]
#   end
#  end
# end

# 修正後
RSpec.describe 'TrashObjectsAPI', type: :request do
  describe 'GET /api/v1/trash_objects/index' do
    it 'returns successfully' do
      get api_v1_trash_objects_index_path

      # ....[省略]
    end
  end
end

以上・・・かな?

以上です!使っているライブラリや構造によって他にも変更箇所は増える可能性があるとは思いますが、基本的な箇所は網羅できていると思います。

宣伝: Railsアプリの作り方を学べる教材を公開しています

Techpit様にて、Ruby on RailsとVue.jsでリアルタイムチャットアプリを作る教材を公開しています。
devise_token_authを使ったトークン認証、HTTP通信やAction Cableを使用したWebsocket通信についても詳しく説明しています。Rails APIモードでアプリケーションを作って見たい方はぜひ購入してください!

参考サイト

109
85
3

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
109
85