You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
936 B

require 'fastercsv'
class Map < ActiveRecord::Base
named_scope :lowest_scoring, :conditions => [
%{`maps`.`points` = (
SELECT `points` FROM `maps`
ORDER BY `points` ASC
LIMIT 1
)}]
def self.from_city_name(city_name)
self.find_or_initialize_by_name(city_name).save!
end
def self.pair
if Mash.count == 0
self.all(:order => 'RANDOM()', :limit => 2)
else
self.lowest_scoring.all(:order => 'RANDOM()', :limit => 2)
end
end
def self.import(csv_filename)
FasterCSV.parse(open(csv_filename), :headers => true,
:header_converters => [:downcase, :symbol]).each do |row|
map = self.find_or_initialize_by_name(
"#{row[:city]}, #{row[:region]}"
)
map.save
if block_given?
yield map
end
end
end
def rounds
Mash.count(:conditions => ['map_a = :id OR map_b = :id', {:id => self.id}])
end
end