Class: SportDB::Models::Team

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sportdb/models/team.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_ary!(teams, more_values = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sportdb/models/team.rb', line 20

def self.create_from_ary!( teams, more_values={} )
  teams.each do |values|
    
    ## key & title required
    attr = {
      key: values[0]
    }

    ## title (split of optional synonyms)
    # e.g. FC Bayern Muenchen|Bayern Muenchen|Bayern
    titles = values[1].split('|')
    
    attr[ :title ]    =  titles[0]
    ## add optional synonyms
    attr[ :synonyms ] =  titles[1..-1].join('|')  if titles.size > 1

    
    attr = attr.merge( more_values )
    
    ## check for optional values
    values[2..-1].each do |value|
      if value.is_a? Country
        attr[ :country_id ] = value.id
      elsif value.is_a? City
        attr[ :city_id ] = value.id 
      elsif value.length == 3   ## assume its a tag (three letters e.g. ITA)
        attr[ :tag ] = value
      elsif value =~ /^city:/   ## city:
        value_city_key = value[5..-1]  ## cut off city: prefix
        value_city = City.find_by_key!( value_city_key )
        attr[ :city_id ] = value_city.id
      else
        attr[ :title2 ] = value
      end
    end

    ## check if exists
    team = Team.find_by_key( values[0] )
    if team.present?
      puts "*** warning team with key '#{values[0]}' exists; skipping create"
    else      
      Team.create!( attr )
    end
  end # each team
end

Instance Method Details

#gamesObject

fix - how to do it with has_many macro? possible??



10
11
12
# File 'lib/sportdb/models/team.rb', line 10

def games
  Game.where( 'team1_id = ? or team2_id = ?', id, id ).order( 'play_at' ).all
end