# == Schema Information # # Table name: completions # # id :integer not null, primary key # display_relative_time :string # display_time_from_start :string # time_seconds :integer # created_at :datetime not null # updated_at :datetime not null # contest_id :integer not null # contestant_id :integer not null # puzzle_id :integer not null # # Indexes # # index_completions_on_contest_id (contest_id) # index_completions_on_contestant_id (contestant_id) # index_completions_on_puzzle_id (puzzle_id) # # Foreign Keys # # contest_id (contest_id => contests.id) # contestant_id (contestant_id => contestants.id) # puzzle_id (puzzle_id => puzzles.id) # class Completion < ApplicationRecord belongs_to :contest belongs_to :contestant belongs_to :puzzle before_save :add_time_seconds validates :display_time_from_start, presence: true, format: { with: /\A((\d\d|\d):\d\d|\d\d|\d):\d\d\z/ } validates :puzzle_id, uniqueness: { scope: :contestant } def add_time_seconds arr = display_time_from_start.split(":") if arr.size == 3 self.time_seconds = arr[0].to_i * 3600 + arr[1].to_i * 60 + arr[2].to_i elsif arr.size == 2 self.time_seconds = arr[0].to_i * 60 + arr[1].to_i end end end