LoginSignup
2
2

More than 5 years have passed since last update.

rails new したときのオプションを自動的に保存する

Posted at

Railsはソースコードの自動生成が充実しているので、実行したコマンドを記録しておくと何かと便利です。コマンドを再利用する場合や、「どいういうオプションで実行したっけ?」という場合に、後から参照することが可能となります。

以下の方法は、rails newで新規にプロジェクトを作成する際に使用したオプションを、自動的に保存しておく方法です。副作用はありません。ぜひ設定してください。

まず、お使いのtemplate.rbの先頭に以下のように記述します。template.rbが無い場合は、このまま保存して使ってください。

template.rb
args = self.instance_eval { @_initializer[0, 2] }.flatten
create_file "memo.txt",  <<END.strip_heredoc
  rails new #{args.join ' '}
END

実行方法

-mオプションで、先ほどのtemplate.rbを指定してrails newを実行します。

$ rails new hoge -T -d mysql -m template.rb

実行結果

プロジェクトルートに、以下のような内容のファイルが作られます。

memo.txt
rails new hoge -T -d mysql -m template.rb

Tips

memo.txtには、rails gの結果も保存しておくと、migrationを作り直す時など(indexを付け忘れたことに後から気づくとか)に何かと便利です。

memo.txt
rails new hoge -T -d mysql -m template.rb

rails g scaffold Book \
  title \
  isbn:index \
  author:belongs_to{required}
2
2
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
2
2