我有一个rake任务,它有一个相当长的定义。我试着采用相当长的指令集并将它们分成如下函数:
#lib/tasks/helpme.rake
require 'rake'
require 'rest-client'
require 'warbler'
task :helpme => :environment do
# some stuff
some_function("a","b")
end
# (MyCoolModel < ActiveRecord:Base is defined in app/models/my_cool_model.rb)
def some_function (p1,p2)
m=MyCoolModel.new
m.p1=p1
m.p2=p2
# 20 more lines of very cool code
end
当我运行“rake helpme”时,这不起作用。它抱怨MyCoolModel无法访问。 然后我尝试了以下内容,知道如何在Ruby中做各种奇怪的事情:
#lib/tasks/helpme.rake
require 'rake'
require 'rest-client'
require 'warbler'
task :helpme => :environment do
# some stuff
# (MyCoolModel < ActiveRecord:Base is defined in app/models/my_cool_model.rb)
def some_function (p1,p2)
m=MyCoolModel.new
m.p1=p1
m.p2=p2
# 20 more lines of very cool code
end
some_function("a","b")
end
当我跑“耙帮助”时,它运作得很好。
我的问题:如何在自定义rake任务调用的函数中访问模型类,而无需在任务代码的代码中编写函数?