【发布时间】:2014-06-01 14:53:51
【问题描述】:
我在应用程序控制器中有以下代码:
class ApplicationController < ActionController::Base
before_filter :find_today_forecast
...
def find_today_forecast
@today_forecast = Weather.displayed.city(@city).first
end
end
变量@today_forecast 仅在我在生产和开发环境中重新启动应用程序时更新。
Rails.cache.clear 和 rake tmp:clear 不起作用。有什么问题
更新,天气模型和 YandexWeather 解析器:
class Weather < ActiveRecord::Base
DISPLAY_COUNT = 7
belongs_to :city
scope :city, lambda {|field| {:conditions => {:city_id => field}}}
validates_presence_of :temp_high, :temp_low
validates_uniqueness_of :date, :scope => :city_id
scope :displayed, :conditions => ['date >= ?', Date.today], :order => 'date ASC', :limit => Weather::DISPLAY_COUNT
def high
self.temp_high.to_i > 0 ? '+' + self.temp_high.to_s : self.temp_high
end
def low
self.temp_low.to_i > 0 ? '+' + self.temp_low.to_s : self.temp_low
end
# загружаем погоду из Яндекса
def self.load(city)
begin
YandexWeather.new(city).create_weathers
rescue
Rails.logger.info "no weather for city - #{city.title}"
end
end
end
class YandexWeather
ICON_NAMES = {
'ovc_ra' => 'heavy-rain',
'ovc_-ra' => 'heavy-rain',
'bkn_na' => 'heavy-cloud-day',
'skc_n' => 'sun',
'skc_d' => 'night',
'ovc' => 'heavy-cloud-day',
'bkn_-ra_n' => 'heavy-rain',
'bkn_d' => 'medium-cloud',
'bkn_n' => 'medium-cloud-night',
'bkn_-ra_d' => 'rain',
'ovc_-sn' => 'snow',
'ovc_ts_ra' => 'heavy-storm',
'bkn_-sn_n' => 'snow',
'ovc_sn' => 'snow',
'bkn_-sn_d' => 'chance_of_snow',
'bkn_ra_d' => 'rain',
'bkn_ra_n' => 'rain'
}
PER_PAGE = 10
def initialize city
@agent = Mechanize.new
@city = city
end
...
end
【问题讨论】:
-
能把
displayed和city的代码也加进去吗? -
需要查看您的
Weather模型。 -
添加了天气模型。天气与 yandex 天气解析器对话。从 cron 开始的解析器。
标签: ruby-on-rails ruby ruby-on-rails-3