railsとrakeの違い。なぜ同じ処理をするのにコマンドが2つあるのか?(rake db:migrateとrails db:migrateの違い)

What are thire differences, rails, rake, bin/rails and bundle exex rails? Rails
記事内に広告が含まれていることがあります。

Railsで実行したいコマンドを調べていると、 同じ処理をするにしても、rakeで始まるコマンドと、railsで始まるコマンドの2つが出てきます。

例えば、マイグレーションを実行する、db:migration だと以下のようになります。

rake db:migrate
rails db:migrate


他にも、bin/rails, bundle exec rails といった表記も出てきます。これらがそれぞれどういった内容になっているのかを解説しています。

先に結論から言うと、Rails 5以降では、railsコマンドのみを使えば問題ありません

rakeとrailsのそれぞれの違い

rakeとrailsのそれぞれの違いとして一番大きいのはrailsのバージョンの違いです。

Rails 5から、railsのみで対応できるようになりました(rakeが不要になりました)。
Rails 4以前は、コマンドによって、rakeを使うものと、railsを使うものが区別されています。

Rails4まではrakeとrailsが使い分けられていたのですが、煩雑なので、rails5からrakeがrailsに統合されました。

Rails 5の5.0.1がリリースされたのは、2016/12/21なので、もはやrakeは過去のものといえます。基本的にはrailsのみで問題ありません

(参考)Ruby on Railsのバージョンとリリース日一覧
(参考)Ruby on Rails 5.0 リリースノート

rakeとrailsの使い分け

今となってはほぼ必要ないのですが、補足として、Rails 4までrakeとrailsをどう使い分けていたかについてです。

rakeコマンド

rakeは、rails以外のファイルや処理をコンパイルしてrails環境で使えるようにするためのコマンドです。

自分の実行環境で使えるようにコンパイルしたり、アプリケーションやライブラリを組み立てることをビルドというため、Railsの公式ページではrakeのことをビルドツールと呼んでいます。

データベースはrailsとは異なるサーバーなので、DBをRailsの環境で実行できるように、マイグレーションをするといった処理は rakeコマンドを使っていました。

rakeで使えるコマンドは $rake -T で一覧が確認できます。

$ rake -T
rake about                              # List versions of all Rails frameworks and the environment
rake assets:clean[keep]                 # Remove old compiled assets
rake assets:clobber                     # Remove compiled assets
rake assets:environment                 # Load asset compile environment
rake assets:precompile                  # Compile all the assets named in config.assets.precompile
rake cache_digests:dependencies         # Lookup first-level dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake cache_digests:nested_dependencies  # Lookup nested dependencies for TEMPLATE (like messages/show or comments/_comment.html)
rake db:create                          # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config)
rake db:drop                            # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config)
rake db:fixtures:load                   # Load fixtures into the current environment's database
rake db:migrate                         # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:status                  # Display status of migrations
rake db:rollback                        # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear              # Clear a db/schema_cache.dump file
rake db:schema:cache:dump               # Create a db/schema_cache.dump file
rake db:schema:dump                     # Create a db/schema.rb file that is portable against any DB supported by AR
rake db:schema:load                     # Load a schema.rb file into the database
rake db:seed                            # Load the seed data from db/seeds.rb
rake db:setup                           # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the database first)
rake db:structure:dump                  # Dump the database structure to db/structure.sql
rake db:structure:load                  # Recreate the databases from the structure.sql file
rake db:version                         # Retrieves the current schema version number
rake doc:app                            # Generate docs for the app -- also available doc:rails, doc:guides (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake log:clear                          # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)


(参考)Rails公式ドキュメント Rakeとは・Rakeコマンド一覧


railsコマンド

Rails 4まででrailsコマンドは、Rails環境の中でファイルやフォルダを作成するコマンドとして使われていました。

例えば、Railsのアプリケーションを作成する rails new、アプリケーションに必要なファイルをまとめて生成する rails scaffold、コントローラーとビューを作成する rails controller、などは元々railsコマンドのみで使われていたものです。

(参考)Rails公式ドキュメント Railsのアプリケーションを作成

主なrailsコマンドは以下になります。

rails new
rails generate scaffold
rails generate controller
rails generate migration
rails generate model
rails server
rails destroy
rails plugin install
rails console
rails dbconsole
rails runner
rails about
rails assets
rails notes
rails routes
rails tmp

bin/rails, bundle exec railsの違い

railsとrake以外にも、bin/rails, bundle exec railsという表記が存在します。これもrailsのコマンドをややこしくしているものです。

結論から言うと、どれも同じ処理になります。

rails db:migrate
bin/rails db:migrate
bundle exec rails db:migrate

railsコマンドは本来、binディレクトリ配下にあります。Railsの場合は、railsと入力すれば、 bin/railsを実行してくれます。

rails = bin/rails というわけです。

このbin/railsが何をしているかというと

  1. bin/railsconfig/bootを読み込みます。
  2. config/bootbundler/setupを読み込みます。
  3. bundler/setup の実行は bundle exec の実行と同じになります。

つまり、 bin/rails = bundle exec というわけです。


参考

タイトルとURLをコピーしました