Skip to content

Instantly share code, notes, and snippets.

@jsmestad
Created April 5, 2009 01:05
Show Gist options
  • Select an option

  • Save jsmestad/90339 to your computer and use it in GitHub Desktop.

Select an option

Save jsmestad/90339 to your computer and use it in GitHub Desktop.
named_scope :in_default_region, lambda{ {
:include => :region,
:conditions => ['regions.id = ?', Region.default]
}}
named_scope :regional_and_or_default, lambda{ |key| {
:include => [:region],
:conditions => ['association = ? AND ( region_id = ? OR region_id = ? )', key, self.current_region, Region.default],
:order => "FIELD(region_id, #{id_or_null( self.current_region )}, #{id_or_null( Region.default )})"
} }
def self.regional_or_default(key)
self.current_region ? self.regional_and_or_default(key).first : self.in_default_region.by_association(key).first
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment