Calling Methods from a Task in Rake Files

Can I call a method that is in the same rake file as the job? In the code below, you can see that I have a get_search_url method that will be assigned to the 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 
+4
source share
1 answer

Yes, it is absolutely possible. Just define these 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 
+8
source

Source: https://habr.com/ru/post/1501659/


All Articles