メニュー

リファレンス

HTMLタグ逆引き

検索

Rake

Rakeとは

Rakeとは

Rubyで記述されたビルドツールです。

コマンド一覧(rake -T)

説明

Railsで使用できるコマンドの一覧を表示

使い方

$ rake -T

$ rake -T
rake about              # List versions of all Rails frameworks and the environment
rake assets:clean       # Remove compiled assets
rake assets:precompile  # Compile all the assets named in config.assets.precompile
rake db:create          # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop            # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load   # Load fixtures into the current environment's database.
rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE=false).
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:dump     # Create a db/schema.rb file that can be portably used 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 db first)
rake db:structure:dump  # Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql
rake db:version         # Retrieves the current schema version number
rake doc:app            # Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake log:clear          # Truncates all *.log files in log/ to zero bytes
rake middleware         # Prints out your Rack middleware stack
rake notes              # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom       # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template     # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update       # Update configs and some other initially generated files (or use just update:configs, update:scripts, or update:application_controller)
rake routes             # Print out all defined routes in match order, with names.
rake secret             # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions).
rake stats              # Report code statistics (KLOCs, etc) from the application
rake test               # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile, test:plugins)
rake test:recent        # Run tests for {:recent=>"test:prepare"} / Test recent changes
rake test:single        # Run tests for {:single=>"test:prepare"}
rake test:uncommitted   # Run tests for {:uncommitted=>"test:prepare"} / Test changes since last checkin (only Subversion and Git)
rake time:zones:all     # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6
rake tmp:clear          # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create         # Creates tmp directories for sessions, cache, sockets, and pids

使用しているライブラリのバージョンを確認(rake about)

説明

使用しているライブラリのバージョンを確認

使い方

$ rake about

$ rake about
About your application's environment
Ruby version              1.9.2 (x86_64-darwin10)
RubyGems version          1.3.7
Rack version              1.4
Rails version             3.2.1
JavaScript Runtime        JavaScriptCore
Active Record version     3.2.1
Action Pack version       3.2.1
Active Resource version   3.2.1
Action Mailer version     3.2.1
Active Support version    3.2.1
Middleware                ActionDispatch::Static, ...
Application root          /xxx
Environment               development
Database adapter          sqlite3
Database schema version   0

ソースコードからドキュメントを生成(rake doc:app)

説明

決められた記述で書かれたコメントやクラス定義からドキュメントを生成する。生成したドキュメントは、/doc/app/indec/htmlからアクセスする。

使い方

$ rake doc:app

$ rake doc:app
rm -r doc/app
Parsing sources...
100% [ 3/ 3]  app/helpers/application_helper.rb

Generating Darkfish format into /xxx/rails/xxx/doc/app...

Files:      3

Classes:    1 (1 undocumented)
Modules:    1 (1 undocumented)
Constants:  0 (0 undocumented)
Attributes: 0 (0 undocumented)
Methods:    0 (0 undocumented)

Total:      2 (2 undocumented)
  0.00% documented

Elapsed: 0.3s

Railsのドキュメントを生成(rake doc:rails)

説明

Railsのドキュメントを生成

使い方

$ rake doc.rails

基本形(オプションなし)
$ rake doc.rails

ソースコードにTODOを記入(rake notes)

説明

ソースコードに記入された「FIXME, OPTIMIZE, TODO」を検索して表示

使い方

$ rake notes

ソースコードにやり残しを記述
def test
# TODO: やり残し
end
$ rake notes
  * [  3] [TODO] やり残し

データベースを削除(rake db:drop)

説明

datebase.ymlの設定に従って、データベースを削除する。

使い方

環境ごと
$ rake db:drop [RAILS_ENV=環境(development, text, production)]
すべて
$ rake db:drop:all

基本的な使い方
$ rake db:drop
production環境のデータベースを削除
$ rake db:drop RAILS_ENV=production

マイグレーションの実行(rake db:migrate)

説明

未実行のマイグレーションファイルを実行

使い方

$ rake db:migrate [VERSION=バージョン番号] [オプション]

実行の流れ

  1. rake db:migrateを実行
  2. schema_migrationsテーブルを調べ、存在しなければ作成
  3. db/migrateディレクトリ内のすべてのマイグレーションファイルを調べる
  4. データベースの現在のバージョンと異なるバージョンがあった場合、データベースに適応
  5. schema_migrationsテーブルの更新

詳細

コマンド説明
rake db:abort_if_pending_migrations実行されていないmigrationを表示
rake db:migrate [VERSION=バージョン番号] [オプション]db/migrate内のスクリプトファイルからdatabaseにテーブル作成
rake db:migrate:down指定したmigrationファイルのself.downメソッドを実行
rake db:migrate:redo [STEP=ステップ数]指定したmigrationファイルのself.downメソッドを実行
rake db:migrate:resetdatabaseを一度削除してもう一度作成し、db:migrate実行
rake db:mgrate:up指定したmigrationファイルのself.upメソッドを実行

オプション

オプション説明デフォルト
RAILS_ENVtestやproduction環境の設定を使用する場合に使用development
VERBOSE途中結果を表示するかfalse

基本形(オプションなし)
$ rake db:migrate
プロダクション環境のマイグレーション
$ rake db:migrate RAILS_ENV=production
テスト環境のマイグレーション
$ rake db:migrate RAILS_ENV=test
特定のバージョンのスキーマに変更
$ rake db:migrate VERSION=201010190000
テーブルの初期化
$ rake db:migrate:reset
一つ前のバージョンに戻す
$ rake db:migrate:redo STEP=1
指定したマイグレーションのみ実行
$ rake db:migrate:up VERSION=201010190000
スキーマのバージョンを調べる
$ rake db:version

マイグレーションの履歴(rake db:migrate:status)

説明

マイグレーションの履歴を表示する

使い方

$ rake db:migrate:status [RAILS_ENV=環境(development, text, production)]

基本形(オプションなし)
$ rake db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20110521181506  Create pages
   up     20110521182915  Create categories
   up     20110703120321  Add position to pages
   up     20110716180121  Add position to categories
  down  20110724110158  Add english name to category

マイグレーションのバージョンを確認(rake db:version)

説明

マイグレーションのバージョンを表示する

使い方

$ rake db:version [RAILS_ENV=環境(development, text, production)]

基本形(オプションなし)
$ rake db:version
Current version: 20110716180121

スキーマファイルでデータベースを作成(rake db:schema:load)

説明

スキーマファイルでデータベースを作成する

使い方

$ rake db:schema:load [RAILS_ENV=環境(development, text, production)]

基本形(オプションなし)
$ rake db:schema:load

現在のデータベースからスキーマファイルを生成(rake db:schema:dump)

説明

現在のデータベースからスキーマファイルを生成する

使い方

$ rake db:schema:dump [RAILS_ENV=環境(development, text, production)]

基本形(オプションなし)
$ rake db:schema:dump

データベースに初期データを挿入(rake db:seed)

説明

シードファイルを使ってデータベースに初期データを挿入する

使い方

$ rake db:seed [RAILS_ENV=環境(development, text, production)]

基本的な使い方
$ rake db:seed
production環境のデータベースにデータを挿入
$ rake db:seed RAILS_ENV=production

データベースにテストデータを挿入(rake db:fixtures:load)

説明

フィクスチャファイルを使って、データベースにテストデータを挿入する

使い方

$ rake db:fixtures:load [FIXTURES=フィクスチャファイル名 RAILS_ENV=環境]

基本形(オプションなし)
$ rake db:fixtures:load

データベースを作製(rake db:create)

説明

datebase.ymlの設定に従って、データベースを作製する。

使い方

環境ごと
$ rake db:create [RAILS_ENV=環境(development, text, production)]
すべて
$ rake db:create:all

基本形(オプションなし)
$ rake db:create

ログファイルを削除(rake log:clear)

説明

logフォルダ以下にある、ログファイルを削除する

使い方

$ rake log:clear

基本形(オプションなし)
$ rake log:clear

Railsのバージョンアップ(rake rails:update)

説明

Railsのバージョンアップ

使い方

$ rake rails:update

基本形(オプションなし)
$ rake rails:update

自動生成されるテンプレートをカスタマイズ(rake rails:templates:copy)

説明

自動生成されるテンプレートをカスタマイズ

使い方

$ rake rails:templates:copy

基本形(オプションなし)
$ rake rails:templates:copy

マイグレーションをロールバック(rake db:rollback)

説明

マイグレーションを前のバージョンに戻す

使い方

$ rake db:rollback

基本形(オプションなし)
$ rake db:rollback
3つ前にロールバック
$ rake db:rollback STEP=3

Rakeファイルを自作

Rakeファイルは自作することもできる

データベースに初期データを投入(rake db:seed)

説明

データベースに初期データを投入する。

使い方

$ rake db:seed

書き方

1件ずつ記述
# db/seeds.rb
Category.create(:name => "Railsの基礎知識", :position => 1)
Category.create(:name => "Rubyの基礎知識", :position => 2)
まとめて記述
# db/seeds.rb
5.times do |i|
     Page.create(:name => "テスト #{i}", :category_id => 1)
end