Module: ActiveRecord::Core::ClassMethods
- Defined in:
- lib/active_record/core.rb
Instance Method Summary collapse
-
#===(object) ⇒ Object
Overwrite the default class equality method to provide support for association proxies.
- #allocate ⇒ Object
-
#arel_engine ⇒ Object
Returns the Arel engine.
-
#arel_table ⇒ Object
Returns an instance of
Arel::Table
loaded with the current table name. -
#find(*ids) ⇒ Object
:nodoc:.
-
#find_by(*args) ⇒ Object
:nodoc:.
-
#find_by!(*args) ⇒ Object
:nodoc:.
- #generated_association_methods ⇒ Object
-
#inherited(child_class) ⇒ Object
:nodoc:.
-
#initialize_find_by_cache ⇒ Object
:nodoc:.
-
#initialize_generated_modules ⇒ Object
:nodoc:.
-
#inspect ⇒ Object
Returns a string like ‘Post(id:integer, title:string, body:text)’.
Instance Method Details
#===(object) ⇒ Object
Overwrite the default class equality method to provide support for association proxies.
228 229 230 |
# File 'lib/active_record/core.rb', line 228 def ===(object) object.is_a?(self) end |
#allocate ⇒ Object
112 113 114 115 |
# File 'lib/active_record/core.rb', line 112 def allocate define_attribute_methods super end |
#arel_engine ⇒ Object
Returns the Arel engine.
242 243 244 245 246 247 248 249 |
# File 'lib/active_record/core.rb', line 242 def arel_engine # :nodoc: @arel_engine ||= if Base == self || connection_handler.retrieve_connection_pool(self) self else superclass.arel_engine end end |
#arel_table ⇒ Object
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
237 238 239 |
# File 'lib/active_record/core.rb', line 237 def arel_table # :nodoc: @arel_table ||= Arel::Table.new(table_name, arel_engine) end |
#find(*ids) ⇒ Object
:nodoc:
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/active_record/core.rb', line 126 def find(*ids) # :nodoc: # We don't have cache keys for this stuff yet return super unless ids.length == 1 # Allow symbols to super to maintain compatibility for deprecated finders until Rails 5 return super if ids.first.kind_of?(Symbol) return super if block_given? || primary_key.nil? || default_scopes.any? || current_scope || 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 s = find_by_statement_cache[key] || find_by_statement_cache.synchronize { find_by_statement_cache[key] ||= StatementCache.create(connection) { |params| where(key => params.bind).limit(1) } } record = s.execute([id], self, connection).first unless record raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}" end record rescue RangeError raise RecordNotFound, "Couldn't find #{name} with an out of range value for '#{primary_key}'" end |
#find_by(*args) ⇒ Object
:nodoc:
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/active_record/core.rb', line 162 def find_by(*args) # :nodoc: return super if current_scope || !(Hash === args.first) || reflect_on_all_aggregations.any? return super if default_scopes.any? hash = args.first return super if hash.values.any? { |v| v.nil? || Array === v || Hash === 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) } key = hash.keys klass = self s = find_by_statement_cache[key] || find_by_statement_cache.synchronize { find_by_statement_cache[key] ||= StatementCache.create(connection) { |params| wheres = key.each_with_object({}) { |param,o| o[param] = params.bind } klass.where(wheres).limit(1) } } begin s.execute(hash.values, self, connection).first rescue TypeError => e raise ActiveRecord::StatementInvalid.new(e., e) rescue RangeError nil end end |
#find_by!(*args) ⇒ Object
:nodoc:
195 196 197 |
# File 'lib/active_record/core.rb', line 195 def find_by!(*args) # :nodoc: find_by(*args) or raise RecordNotFound.new("Couldn't find #{name}") end |
#generated_association_methods ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/active_record/core.rb', line 203 def generated_association_methods @generated_association_methods ||= begin mod = const_set(:GeneratedAssociationMethods, Module.new) include mod mod end end |
#inherited(child_class) ⇒ Object
:nodoc:
121 122 123 124 |
# File 'lib/active_record/core.rb', line 121 def inherited(child_class) # :nodoc: child_class.initialize_find_by_cache super end |
#initialize_find_by_cache ⇒ Object
:nodoc:
117 118 119 |
# File 'lib/active_record/core.rb', line 117 def initialize_find_by_cache # :nodoc: self.find_by_statement_cache = {}.extend(Mutex_m) end |
#initialize_generated_modules ⇒ Object
:nodoc:
199 200 201 |
# File 'lib/active_record/core.rb', line 199 def initialize_generated_modules # :nodoc: generated_association_methods end |
#inspect ⇒ Object
Returns a string like ‘Post(id:integer, title:string, body:text)’
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/active_record/core.rb', line 212 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 = columns.map { |c| "#{c.name}: #{c.type}" } * ', ' "#{super}(#{attr_list})" else "#{super}(Table doesn't exist)" end end |