我将以下内容存储在 rake 任务中。基本上,如果来自 url 的响应代码不是 200,我需要将“已发布”属性从 1 更新为 0。当我运行此命令时,我得到:undefined method 'request_uri' for #<URI::Generic:0x00000102136408 URL:url>
desc "Problem products"
task :update_broken_links => :environment do
require 'net/http'
Product.find(:all, :conditions =>["published = ? AND feed_id = ?", '1', 820]).each do |product|
url = product.url
uri = URI('url')
response = Net::HTTP.get_response(uri)
x = response.code.to_i
if x != 200
published = '0'
else
published = product.published
end
product.update_attribute(:publsihed, publsihed)
end
end