You can use cron. Look at https://github.com/javan/whenever (Cron jobs in Ruby)
Saving rake task in database to add in view
-
31-05-2022 - |
Question
I am trying to save a rake task into my database to I can display the content in my view.
Here's the task:
desc "Fetch Digital Trends"
task :fetch_trends => :environment do
require 'nokogiri'
require 'httparty'
url = "http://www.digitaltrends.com/computing/"
doc = Nokogiri::HTML(open(url))
doc.css(".post").each do |item|
title = item.at_css(".content h3").text
link = item.at_css(".content h3 a")[:href]
puts "#{title} - #{link}"
end
end
It is possible to make this task run every hour to update the content?
La solution
Autres conseils
You can do it in two ways.
Method 1:
Read from database.
Write to a file in lib/tasks/name.rake
load the file
Execute the rake task
Method 2:
Load the string from the database
Use eval
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow