# == Schema Information # # Table name: world_participants # # id :integer not null, primary key # final_result :integer # name :string not null # qualifier_result :integer # round :string not null # score :integer # semifinal_result :integer # username :string not null # created_at :datetime not null # updated_at :datetime not null # world_country_id :integer not null # # Indexes # # index_world_participants_on_world_country_id (world_country_id) # # Foreign Keys # # world_country_id (world_country_id => world_countries.id) # class WorldParticipant < ApplicationRecord belongs_to :world_country validates :name, presence: true validates :username, presence: true validates :round, inclusion: { in: %w[a b c d], message: "%{value} is not a valid round" } validates :qualifier_result, numericality: { greater_than_or_equal_to: 1 }, if: -> { qualifier_result.present? } validates :semifinal_result, numericality: { greater_than_or_equal_to: 1 }, if: -> { semifinal_result.present? } validates :final_result, numericality: { greater_than_or_equal_to: 1 }, if: -> { final_result.present? } before_save :compute_score def compute_score self.score = WorldBet.points(qualifier_result) + WorldBet.points(semifinal_result) * 2 + WorldBet.points(final_result) * 4 end def self.add_participants(str) str.split(";", -1).each do |c| cs = c.split(",", -1) if cs.length >= 4 name = cs.first username = cs[1] country_code = cs[2] round = cs.last country = WorldCountry.where(code: country_code).first if country WorldParticipant.create(name: name, username: username, world_country: country, round: round) end end end end end