rake任务无法访问rails.cache

我想从一个将远程天气数据存储在rails缓存中的cron作业调用rake任务。 但是,我必须在这里做一些非常错误的事情,因为我无法通过无数无结果的搜索找到任何解决方案。

假设我定义并调用此任务

namespace :weather do desc "Store weather from remote source to cache" task :cache do Rails.cache.write('weather_data', Date.today) end end 

我收到了错误

 Anonymous modules have no name to be referenced by 

这让我相信rails缓存不可用。 从rake文件输出Rails.class给了我Module但Rails.cache.class再次返回上面的错误。 我需要在这里加一些东西吗? 我在互联网上绝望吗? 🙂

提前致谢。

尝试传递rails环境

 task (:cache => :environment) do ... end 

好像你会得到一个不同的错误,但我会试试这个