[英]How to import Model into a custom rake task?
在我的lib / tasks文件夾中,添加了一個新的.rake文件。
在rake任務中,我正在這樣做:
p = Post.new( ....)
p.save!
運行任務時,出現錯誤:
rake aborted!
uninitialized constant Post
導入Post模型需要做什么?
我認為您可能缺少環境聲明。 為了使Rake知道您的Rails環境,這是必需的。 您的rake任務調用應如下所示:
task :my_rake_task => [:environment] do
# Your code here
end
讓我知道是否可以解決問題!
您要使任務依賴於rails環境。 您可以通過在任務聲明后指定=>:environment來實現:
namespace :my_task do
desc "an example task"
task :create_post => :environment do
Post.new .... # the rest of the implementation
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.