3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

rails console と bin/rails console の違い

Posted at
Page 1 of 3

はじめに

rails初学者です。

$bin/rails console 

学習をしていて時々見かけるこのbinにはどのような意味があるのだろうかと疑問に思い、調べましたので簡単にまとめます。

結論

bin/rails console は
”bin/rails の設定を全部適用した上で rails console を起動してね”
という命令を意味している

bin とは何

ほとんど開いたことがなかったのですが、Railsアプリの中にbinというディレクトリが存在します。

Image from Gyazo

ここのbin/railsファイルの中身を見てみるとこのように書かれています。

#!/usr/bin/env ruby
begin
  load File.expand_path('../spring', __FILE__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'

上から簡単に説明しますと、

  • #!/usr/bin/env ruby
    これは rubyを使うよ という宣言になります。
      

  • begin load File.expand_path('../spring', __FILE__)
    springファイルがあればそれを読み込んでくださいという内容です。
      

  • APP_PATH = File.expand_path('../config/application', __dir__)
    config/applicationにある設定をもとに動きますよ!という意味です。
      

  • require_relative '../config/boot'
    ここで起動の準備をしています。このアプリ専用のgemを読み込むために必要です。
      

  • require 'rails/commands'
    ここで Rails が、
    consoleなどの“コマンド機能” を読み込みます。

つまりbin/railsによってそのアプリの設定が反映された状態でconsoleを起動することができるということになります。

consoleの起動以外に

  • サーバーの起動
    bin/rails server

  • DBマイグレーション
    bin/rails db:migrate

  • テストの実行
    bin/rails test

など様々なコマンドにつけたりします。
Railsの完全な環境で起動するために bin/rails をつけた方が良いという解釈で良さそうです。


参考リンク
binって何?railsとrakeって何?(前編)
Railsガイド コマンドツール

※なお誤った解釈がありましたら、ご教授ください!

3
0
1

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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?