0

I have following code in application controller:

class ApplicationController < ActionController::Base
  before_filter :find_today_forecast
  ...
  def find_today_forecast
    @today_forecast = Weather.displayed.city(@city).first
  end
end

variable @today_forecast updates only when I am restarting application on production and development environment.

Rails.cache.clear and rake tmp:clear doesn't work. What is the problem

Update, Weather models and YandexWeather parser:

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
Voldemar Duletskiy
  • 981
  • 1
  • 11
  • 30

1 Answers1

2

the problem is in your displayed scope

scope :displayed, :conditions => ['date >= ?', Date.today], :order => 'date ASC', :limit => Weather::DISPLAY_COUNT

Date.today only gets evaluated the first time.

you need to change this to a lambda scope in order to evaluate it on each call.

scope :displayed, -> {
  where("date >= ?", Date.today).
  order("date ASC").limit(Weather::DISPLAY_COUNT)
}

In general, whenever you have a changing part in your scope you should implement it as a lambda ( -> ) scope.

xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • Sorry for asking this.May I know in which Rails version is `lambda` replaced as `->` symbol? Can you provide me a source? – Pavan Apr 17 '14 at 11:29
  • no prob :-). It is rails 4 style but `lambda` can still be used. Check this out http://stackoverflow.com/questions/16588077/scopes-with-lambda-and-arguments-in-rails-4-style . You can choose which ever alias you like – xlembouras Apr 17 '14 at 11:33
  • Are you referring to me? I'm not the OP :) – Pavan Apr 17 '14 at 11:39
  • `->` is another syntax for `lambda` and was introduced in Ruby 1.9. This is not a replace for `lambda` nor has it something to do with Rails. – spickermann Apr 17 '14 at 11:47