module ActiveRecord::Core::ClassMethods
def ===(object) # :nodoc:
Overwrite the default class equality method to provide support for decorated models.
def ===(object) # :nodoc: object.is_a?(self) end
def _internal? # :nodoc:
def _internal? # :nodoc: false end
def arel_attribute(name, table = arel_table) # :nodoc:
def arel_attribute(name, table = arel_table) # :nodoc: name = name.to_s name = attribute_aliases[name] || name table[name] end
def arel_table # :nodoc:
end
scope :published_and_commented, -> { published.and(arel_table[:comments_count].gt(0)) }
class Post < ActiveRecord::Base
Returns an instance of Arel::Table loaded with the current table name.
def arel_table # :nodoc: @arel_table ||= Arel::Table.new(table_name, type_caster: type_caster) end
def cached_find_by_statement(key, &block)
def cached_find_by_statement(key, &block) cache = @find_by_statement_cache[connection.prepared_statements] cache.compute_if_absent(key) { StatementCache.create(connection, &block) } end
def filter_attributes
def filter_attributes if defined?(@filter_attributes) @filter_attributes else superclass.filter_attributes end end
def find(*ids) # :nodoc:
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? || columns_hash.key?(inheritance_column) && !base_class? id = ids.first return super if StatementCache.unsupported_value?(id) key = primary_key statement = cached_find_by_statement(key) { |params| where(key => params.bind).limit(1) } record = statement.execute([id], connection)&.first unless record raise RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id) end record end
def find_by(*args) # :nodoc:
def find_by(*args) # :nodoc: return super if scope_attributes? || reflect_on_all_aggregations.any? || columns_hash.key?(inheritance_column) && !base_class? hash = args.first return super if !(Hash === hash) || hash.values.any? { |v| StatementCache.unsupported_value?(v) } # We can't cache Post.find_by(author: david) ...yet return super unless hash.keys.all? { |k| columns_hash.has_key?(k.to_s) } keys = hash.keys statement = cached_find_by_statement(keys) { |params| wheres = keys.each_with_object({}) { |param, o| o[param] = params.bind } where(wheres).limit(1) } begin statement.execute(hash.values, connection)&.first rescue TypeError raise ActiveRecord::StatementInvalid end end
def find_by!(*args) # :nodoc:
def find_by!(*args) # :nodoc: find_by(*args) || raise(RecordNotFound.new("Couldn't find #{name}", name)) end
def generated_association_methods # :nodoc:
def generated_association_methods # :nodoc: @generated_association_methods ||= begin mod = const_set(:GeneratedAssociationMethods, Module.new) private_constant :GeneratedAssociationMethods include mod mod end end
def inherited(child_class) # :nodoc:
def inherited(child_class) # :nodoc: # initialize cache at class definition for thread safety child_class.initialize_find_by_cache super end
def initialize_find_by_cache # :nodoc:
def initialize_find_by_cache # :nodoc: @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new } end
def initialize_generated_modules # :nodoc:
def initialize_generated_modules # :nodoc: generated_association_methods end
def inspect # :nodoc:
Returns a string like 'Post(id:integer, title:string, body:text)'
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
def predicate_builder # :nodoc:
def predicate_builder # :nodoc: @predicate_builder ||= PredicateBuilder.new(table_metadata) end
def relation
def relation relation = Relation.create(self) if finder_needs_type_condition? && !ignore_default_scope? relation.where!(type_condition) relation.create_with!(inheritance_column.to_s => sti_name) else relation end end
def table_metadata
def table_metadata TableMetadata.new(self, arel_table) end
def type_caster # :nodoc:
def type_caster # :nodoc: TypeCaster::Map.new(self) end