123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::Core::ClassMethods

Relationships & Source Files
Defined in: activerecord/lib/active_record/core.rb

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#filter_attributes (rw)

Returns columns which shouldn’t be exposed while calling #inspect.

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 322

def filter_attributes
  if @filter_attributes.nil?
    superclass.filter_attributes
  else
    @filter_attributes
  end
end

#filter_attributes=(filter_attributes) (rw)

Specifies columns which shouldn’t be exposed while calling #inspect.

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 331

def filter_attributes=(filter_attributes)
  @inspection_filter = nil
  @filter_attributes = filter_attributes
end

Instance Method Details

#arel_table

This method is for internal use only.

Returns an instance of ::Arel::Table loaded with the current table name.

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 364

def arel_table # :nodoc:
  @arel_table ||= Arel::Table.new(table_name, klass: self)
end

#cached_find_by(keys, values) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 418

def cached_find_by(keys, values)
  statement = cached_find_by_statement(keys) { |params|
    wheres = keys.index_with do |key|
      if key.is_a?(Array)
        [key.map { params.bind }]
      else
        params.bind
      end
    end
    where(wheres).limit(1)
  }

  begin
    statement.execute(values.flatten, connection).first
  rescue TypeError
    raise ActiveRecord::StatementInvalid
  end
end

#cached_find_by_statement(key, &block)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 376

def cached_find_by_statement(key, &block) # :nodoc:
  cache = @find_by_statement_cache[connection.prepared_statements]
  cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
end

#find(*ids)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 243

def find(*ids) # :nodoc:
  # We don't have cache keys for this stuff yet
  return super unless ids.length == 1
  return super if block_given? || primary_key.nil? || scope_attributes?

  id = ids.first

  return super if StatementCache.unsupported_value?(id)

  cached_find_by([primary_key], [id]) ||
    raise(RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}", name, primary_key, id))
end

#find_by(*args)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 256

def find_by(*args) # :nodoc:
  return super if scope_attributes?

  hash = args.first
  return super unless Hash === hash

  hash = hash.each_with_object({}) do |(key, value), h|
    key = key.to_s
    key = attribute_aliases[key] || key

    return super if reflect_on_aggregation(key)

    reflection = _reflect_on_association(key)

    if !reflection
      value = value.id if value.respond_to?(:id)
    elsif reflection.belongs_to? && !reflection.polymorphic?
      key = reflection.join_foreign_key
      pkey = reflection.join_primary_key

      if pkey.is_a?(Array)
        if pkey.all? { |attribute| value.respond_to?(attribute) }
          value = pkey.map do |attribute|
            if attribute == "id"
              value.id_value
            else
              value.public_send(attribute)
            end
          end
          composite_primary_key = true
        end
      else
        value = value.public_send(pkey) if value.respond_to?(pkey)
      end
    end

    if !composite_primary_key &&
      (!columns_hash.key?(key) || StatementCache.unsupported_value?(value))
      return super
    end

    h[key] = value
  end

  cached_find_by(hash.keys, hash.values)
end

#find_by!(*args)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 303

def find_by!(*args) # :nodoc:
  find_by(*args) || where(*args).raise_record_not_found_exception!
end

#generated_association_methods

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 311

def generated_association_methods # :nodoc:
  @generated_association_methods ||= begin
    mod = const_set(:GeneratedAssociationMethods, Module.new)
    private_constant :GeneratedAssociationMethods
    include mod

    mod
  end
end

#inherited(subclass) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 382

def inherited(subclass)
  super

  # initialize cache at class definition for thread safety
  subclass.initialize_find_by_cache
  unless subclass.base_class?
    klass = self
    until klass.base_class?
      klass.initialize_find_by_cache
      klass = klass.superclass
    end
  end

  subclass.class_eval do
    @arel_table = nil
    @predicate_builder = nil
    @inspection_filter = nil
    @filter_attributes = nil
    @generated_association_methods = nil
  end
end

#initialize_find_by_cache

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 239

def initialize_find_by_cache # :nodoc:
  @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new }
end

#initialize_generated_modules

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 307

def initialize_generated_modules # :nodoc:
  generated_association_methods
end

#inspect

This method is for internal use only.

Returns a string like ‘Post(id:integer, title:string, body:text)’

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 348

def inspect # :nodoc:
  if self == Base
    super
  elsif abstract_class?
    "#{super}(abstract)"
  elsif !connected?
    "#{super} (call '#{super}.connection' to establish a connection)"
  elsif table_exists?
    attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ", "
    "#{super}(#{attr_list})"
  else
    "#{super}(Table doesn't exist)"
  end
end

#inspection_filter

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 336

def inspection_filter # :nodoc:
  if @filter_attributes.nil?
    superclass.inspection_filter
  else
    @inspection_filter ||= begin
      mask = InspectionMask.new(ActiveSupport::ParameterFilter::FILTERED)
      ActiveSupport::ParameterFilter.new(@filter_attributes, mask: mask)
    end
  end
end

#predicate_builder

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 368

def predicate_builder # :nodoc:
  @predicate_builder ||= PredicateBuilder.new()
end

#relation (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 404

def relation
  relation = Relation.create(self)

  if finder_needs_type_condition? && !ignore_default_scope?
    relation.where!(type_condition)
  else
    relation
  end
end

#table_metadata (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 414

def 
  TableMetadata.new(self, arel_table)
end

#type_caster

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/core.rb', line 372

def type_caster # :nodoc:
  TypeCaster::Map.new(self)
end