Module: ActiveRecord::Core::ClassMethods

Defined in:
activerecord/lib/active_record/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filter_attributesObject

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


241
242
243
244
245
246
247
# File 'activerecord/lib/active_record/core.rb', line 241

def filter_attributes
  if defined?(@filter_attributes)
    @filter_attributes
  else
    superclass.filter_attributes
  end
end

Instance Method Details

#===(object) ⇒ Object

Overwrite the default class equality method to provide support for decorated models.


269
270
271
# File 'activerecord/lib/active_record/core.rb', line 269

def ===(object) # :nodoc:
  object.is_a?(self)
end

#_internal?Boolean

:nodoc:

Returns:

  • (Boolean)

296
297
298
# File 'activerecord/lib/active_record/core.rb', line 296

def _internal? # :nodoc:
  false
end

#arel_attribute(name, table = arel_table) ⇒ Object

:nodoc:


282
283
284
285
286
# File 'activerecord/lib/active_record/core.rb', line 282

def arel_attribute(name, table = arel_table) # :nodoc:
  name = name.to_s
  name = attribute_aliases[name] || name
  table[name]
end

#arel_tableObject

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

class Post < ActiveRecord::Base
  scope :published_and_commented, -> { published.and(arel_table[:comments_count].gt(0)) }
end

278
279
280
# File 'activerecord/lib/active_record/core.rb', line 278

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

#find(*ids) ⇒ Object

:nodoc:


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'activerecord/lib/active_record/core.rb', line 171

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

#find_by(*args) ⇒ Object

:nodoc:


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'activerecord/lib/active_record/core.rb', line 196

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.index_with { params.bind }
    where(wheres).limit(1)
  }
  begin
    statement.execute(hash.values, connection)&.first
  rescue TypeError
    raise ActiveRecord::StatementInvalid
  end
end

#find_by!(*args) ⇒ Object

:nodoc:


222
223
224
# File 'activerecord/lib/active_record/core.rb', line 222

def find_by!(*args) # :nodoc:
  find_by(*args) || raise(RecordNotFound.new("Couldn't find #{name}", name))
end

#generated_association_methodsObject

:nodoc:


230
231
232
233
234
235
236
237
238
# File 'activerecord/lib/active_record/core.rb', line 230

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

    mod
  end
end

#inherited(child_class) ⇒ Object

:nodoc:


165
166
167
168
169
# File 'activerecord/lib/active_record/core.rb', line 165

def inherited(child_class) # :nodoc:
  # initialize cache at class definition for thread safety
  child_class.initialize_find_by_cache
  super
end

#initialize_find_by_cacheObject

:nodoc:


161
162
163
# File 'activerecord/lib/active_record/core.rb', line 161

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

#initialize_generated_modulesObject

:nodoc:


226
227
228
# File 'activerecord/lib/active_record/core.rb', line 226

def initialize_generated_modules # :nodoc:
  generated_association_methods
end

#inspectObject

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


253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'activerecord/lib/active_record/core.rb', line 253

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

#predicate_builderObject

:nodoc:


288
289
290
# File 'activerecord/lib/active_record/core.rb', line 288

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

#type_casterObject

:nodoc:


292
293
294
# File 'activerecord/lib/active_record/core.rb', line 292

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