use*_*761 4 ruby methods rake ruby-on-rails
It is possible to call a method which is in the same rake file as the task? In the code below you can see that I have the method call get_search_url which will be assigned to url.
namespace :populate do
desc "ETC"
task :db => :environment do
Report.where(link: "").each do |word|
url = get_search_url(word.name)
doc = Nokogiri::HTML(open(url))
word.update_columns(link: link)
end
end
def get_search_url(keyword)
return "URL/keyword"
end
end
Run Code Online (Sandbox Code Playgroud)
是的,绝对有可能.只需在命名空间后定义那些方法;)
namespace :populate do
desc "ETC"
task :db => :environment do
Report.where(link: "").each do |word|
url = get_search_url(word.name)
doc = Nokogiri::HTML(open(url))
word.update_columns(link: link)
end
end
end
def get_search_url(keyword)
return "URL/keyword"
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3963 次 |
| 最近记录: |