Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call methods from a task in Rake files

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
like image 776
user2502761 Avatar asked Jan 12 '23 10:01

user2502761


1 Answers

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
like image 82
Vamsi Krishna Avatar answered Jan 21 '23 15:01

Vamsi Krishna