I am trying to create a named area with the name: current_season, where it will correctly identify the records associated with the year we are in. Itβs basically easy enough, except that I want to use the current year only in June and later, and all until June for use in the previous year.
in rails 3.1. I can easily use:
scope :current_season, lambda { where('season = ?',Time.now.year) } if Time.now.month >= 6
to get the opportunity to work only if we are at the end of the year, and:
scope :current_season, lambda { where('season = ?',Time.now.year - 1) } if Time.now.month < 6
But it seems wasteful to call it all twice and not use the if if else type, or be able to call what I define below to show the exact year, for example:
scope :current_season, lambda { where('season = ?',:current_season_year) } def current_season_year if Time.now.month >= 6 Time.now.year else Time.now.year - 1 end end
But it just laughs at me when I try. Is there a cleaner way? I will also have a scope: last_season and scope: previous_season, most likely, and they will follow the same logic.
Thanks in advance for any advice!
source share