LoginSignup
5

More than 5 years have passed since last update.

rails runnerの使い方

Posted at

rails runnerとは

任意のrubyコードが実行できるコマンドです。Active Recordも使えるのでbatch処理のときに使えます。

ネームスペースの設定

ファイルを指定して実行もできますが、名前空間を設定しましょう。

config/application.rb
config.autoload_paths += %W(#{config.root}/lib)

実行スクリプトを作る

lib/tasks以下にスクリプトを置く。

test.rb
class Tasks:Test
    def self.execute
        print 'test'
    end
end

スクリプトの実行をする

$ rails runnner Tasks::Test.execute

-eオプションで環境が設定できます。

参考

http://qiita.com/3yatsu/items/416411c0a8f696dbf99e
http://qiita.com/necojackarc/items/fb76352dbea5bdd83366#fn1

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
5