application_record.rb 487 Bytes
Newer Older
1 2 3 4
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
Mark Chao's avatar
Mark Chao committed
5 6 7 8

  def self.id_in(ids)
    where(id: ids)
  end
9

10 11 12 13 14 15
  def self.safe_find_or_create_by!(*args)
    safe_find_or_create_by(*args).tap do |record|
      record.validate! unless record.persisted?
    end
  end

16 17 18 19 20 21 22
  def self.safe_find_or_create_by(*args)
    transaction(requires_new: true) do
      find_or_create_by(*args)
    end
  rescue ActiveRecord::RecordNotUnique
    retry
  end
23
end