module CreateIfBestResultForRaceExtension

Public Instance Methods

create_if_best_result_for_race(attributes) click to toggle source
# File app/models/result.rb, line 2
def create_if_best_result_for_race(attributes)
  source_result = attributes[:source_result]
  @owner.scores.each do |score|
    same_race  = (score.source_result.race_id  == source_result.race_id)
    same_person = (score.source_result.person_id == source_result.person_id)
    if same_race && score.source_result.person && same_person
      if attributes[:points] > score.points
        @owner.scores.delete score
      else
        return nil
      end
    end
  end
  create attributes
end