26
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rake Task の中で ActiveRecord 使いたい場合

Last updated at Posted at 2013-02-22
task :hoge => :environment do
  # モデルを使った処理など…
end

という感じで、依存タスクとして:environmentを追加すると良い。

補足

単純にrequire '../../config/environment'としても一応可能だけど、テーブルが無かったりした場合に require に失敗してしまうのでよろしくない。

参考

Do rails rake tasks provide access to ActiveRecord models? - Stack Overflow

26
27
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
26
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?