从Rake文件中的任务调用方法 [英] Call methods from a task in Rake files

查看:62
本文介绍了从Rake文件中的任务调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以调用与任务位于同一rake文件中的方法?在下面的代码中,您可以看到我有一个名为get_search_url的方法,该方法将分配给url.

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

推荐答案

是的,这是绝对可能的.只需在名称空间之后定义这些方法即可;)

Yes it is absolutely possible. Just define those methods after the namespace ;)

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

这篇关于从Rake文件中的任务调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆