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
3
  • can you include the code of displayed and city as well? Commented Apr 17, 2014 at 11:10
  • 1
    Need to see your Weather model. Commented Apr 17, 2014 at 11:12
  • Weather model added. Weather talks with yandex weather parser. Parser starting from cron. Commented Apr 17, 2014 at 11:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry for asking this.May I know in which Rails version is lambda replaced as -> symbol? Can you provide me a source?
no prob :-). It is rails 4 style but lambda can still be used. Check this out stackoverflow.com/questions/16588077/… . You can choose which ever alias you like
Are you referring to me? I'm not the OP :)
-> 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.