Module: ActiveRecord::Core::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#===(object) ⇒ Object

Overwrite the default class equality method to provide support for association proxies.


244
245
246
# File 'activerecord/lib/active_record/core.rb', line 244

def ===(object)
  object.is_a?(self)
end

#allocateObject


133
134
135
136
# File 'activerecord/lib/active_record/core.rb', line 133

def allocate
  define_attribute_methods
  super
end

#arel_attribute(name, table = arel_table) ⇒ Object

:nodoc:


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

def arel_attribute(name, table = arel_table) # :nodoc:
  name = attribute_alias(name) if attribute_alias?(name)
  table[name]
end

#arel_engineObject

Returns the Arel engine.


258
259
260
261
262
263
264
265
# File 'activerecord/lib/active_record/core.rb', line 258

def arel_engine # :nodoc:
  @arel_engine ||=
    if Base == self || connection_handler.retrieve_connection_pool(connection_specification_name)
      self
    else
      superclass.arel_engine
    end
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(self.arel_table[:comments_count].gt(0)) }
end

253
254
255
# File 'activerecord/lib/active_record/core.rb', line 253

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

#find(*ids) ⇒ Object

:nodoc:


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'activerecord/lib/active_record/core.rb', line 148

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.include?(inheritance_column) ||
                  ids.first.kind_of?(Array)

  id  = ids.first
  if ActiveRecord::Base === id
    id = id.id
    ActiveSupport::Deprecation.warn(<<-MSG.squish)
      You are passing an instance of ActiveRecord::Base to `find`.
      Please pass the id of the object by calling `.id`.
    MSG
  end

  key = primary_key

  statement = cached_find_by_statement(key) { |params|
    where(key => params.bind).limit(1)
  }
  record = statement.execute([id], self, connection).first
  unless record
    raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
                             name, primary_key, id)
  end
  record
rescue RangeError
  raise RecordNotFound.new("Couldn't find #{name} with an out of range value for '#{primary_key}'",
                           name, primary_key)
end

#find_by(*args) ⇒ Object

:nodoc:


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'activerecord/lib/active_record/core.rb', line 182

def find_by(*args) # :nodoc:
  return super if scope_attributes? || !(Hash === args.first) || reflect_on_all_aggregations.any?

  hash = args.first

  return super if hash.values.any? { |v|
    v.nil? || Array === v || Hash === v || Relation === 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, self, connection).first
  rescue TypeError
    raise ActiveRecord::StatementInvalid
  rescue RangeError
    nil
  end
end

#find_by!(*args) ⇒ Object

:nodoc:


211
212
213
# File 'activerecord/lib/active_record/core.rb', line 211

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

#generated_association_methodsObject


219
220
221
222
223
224
225
# File 'activerecord/lib/active_record/core.rb', line 219

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

#inherited(child_class) ⇒ Object

:nodoc:


142
143
144
145
146
# File 'activerecord/lib/active_record/core.rb', line 142

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:


138
139
140
# File 'activerecord/lib/active_record/core.rb', line 138

def initialize_find_by_cache # :nodoc:
  @find_by_statement_cache = { true => {}.extend(Mutex_m), false => {}.extend(Mutex_m) }
end

#initialize_generated_modulesObject

:nodoc:


215
216
217
# File 'activerecord/lib/active_record/core.rb', line 215

def initialize_generated_modules # :nodoc:
  generated_association_methods
end

#inspectObject

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


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

def inspect
  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:


272
273
274
# File 'activerecord/lib/active_record/core.rb', line 272

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

#type_casterObject

:nodoc:


276
277
278
# File 'activerecord/lib/active_record/core.rb', line 276

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