rake task can't access rails.cache

点点圈 提交于 2019-12-11 01:47:27

问题


I want to call a rake task from a cron job that stores remote weather data in the rails cache. However, I must be doing something pretty wrong here because I cannot find any solution through countless fruitless searches.

Say I define and call this task

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

I get the error

Anonymous modules have no name to be referenced by

Which leads me to believe the rails cache isn't available. Outputting Rails.class from the rake file gives me Module but Rails.cache.class again returns the above error. Do I need to include something here? Am I just hopeless at internet? :)

Thanks in advance.


回答1:


try passing in the rails environment

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

seems like you would get a different error, but I would try this



来源:https://stackoverflow.com/questions/2791321/rake-task-cant-access-rails-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!