Class: SportDb::Sync::NationalTeam

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/readers/sync/more.rb

Class Method Summary collapse

Class Method Details

.find_or_create(team) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sportdb/readers/sync/more.rb', line 7

def self.find_or_create( team )
  rec = Model::Team.find_by( name: team.name )
  if rec.nil?
    puts "add national team: #{team.key}, #{team.name}, #{team.country.name} (#{team.country.key})"

    ### note: key expected three or more lowercase letters a-z /\A[a-z]{3,}\z/
    attribs = {
      key:        team.key,   ## note: always use downcase fifa code for now!!!
      name:       team.name,
      code:       team.code,
      country_id: Sync::Country.find_or_create( team.country ).id,
      club:       false,
      national:   true  ## check -is default anyway - use - why? why not?
    }

    if team.alt_names.empty? == false
      attribs[:alt_names] = team.alt_names.join('|')
    end

    rec = Model::Team.create!( attribs )
  end
  rec
end