class Place < ActiveRecord::Base def self.by_code_or_default(code) # removes garbage and converts whitespace prefixed codes correctly - like " 71000" -> 71000 valid_code = code.to_i.to_s place = Place.where(postal_code: valid_code).first place ||= Place.where("postal_code is null or postal_code = ''").first return place end end