module BazaModels::Model::Queries::ClassMethods

Public Instance Methods

find(id) click to toggle source
# File lib/baza_models/model/queries.rb, line 7
def find(id)
  row = db.select(table_name, {id: id}, limit: 1).fetch
  raise BazaModels::Errors::RecordNotFound, "Record not found by ID: #{id}" unless row

  new(row, init: true)
end
find_by(where_hash) click to toggle source
# File lib/baza_models/model/queries.rb, line 14
def find_by(where_hash)
  where(where_hash).first
end
find_by!(where_hash) click to toggle source
# File lib/baza_models/model/queries.rb, line 18
def find_by!(where_hash)
  model = find_by(where_hash)
  return model if model
  raise BazaModels::Errors::RecordNotFound, "Record not found by arguments: #{where_hash}" unless model
end
find_or_create_by(data) { |model| ... } click to toggle source
# File lib/baza_models/model/queries.rb, line 31
def find_or_create_by(data)
  model = find_or_initialize_by(data)
  model.save if model.new_record?
  yield model if block_given?
  model
end
find_or_create_by!(data) { |model| ... } click to toggle source
# File lib/baza_models/model/queries.rb, line 38
def find_or_create_by!(data)
  model = find_or_initialize_by(data)
  model.save! if model.new_record?
  yield model if block_given?
  model
end
find_or_initialize_by(data) click to toggle source
# File lib/baza_models/model/queries.rb, line 24
def find_or_initialize_by(data)
  model = find_by(data)
  return model if model

  new(data)
end