Class: ActiveRecord::Reflection::AbstractReflection
- Inherits:
-
Object
- Object
- ActiveRecord::Reflection::AbstractReflection
- Defined in:
- lib/active_record/reflection.rb
Overview
Holds all the methods that are shared between MacroReflection and ThroughReflection.
AbstractReflection
MacroReflection
AggregateReflection
AssociationReflection
HasManyReflection
HasOneReflection
BelongsToReflection
HasAndBelongsToManyReflection
ThroughReflection
PolymorphicReflection
RuntimeReflection
Direct Known Subclasses
MacroReflection, PolymorphicReflection, RuntimeReflection, ThroughReflection
Instance Method Summary collapse
- #alias_candidate(name) ⇒ Object
-
#build_association(attributes, &block) ⇒ Object
Returns a new, unsaved instance of the associated class.
- #build_scope(table, predicate_builder = nil, klass = self.klass) ⇒ Object
- #chain ⇒ Object
- #check_validity_of_inverse! ⇒ Object
-
#class_name ⇒ Object
Returns the class name for the macro.
- #constraints ⇒ Object
- #counter_cache_column ⇒ Object
- #counter_must_be_updated_by_has_many? ⇒ Boolean
-
#has_active_cached_counter? ⇒ Boolean
Returns whether this association has a counter cache and its column values were backfilled (and so it is used internally by methods like
size
/any?
/etc). -
#has_cached_counter? ⇒ Boolean
Returns whether this association has a counter cache.
-
#initialize ⇒ AbstractReflection
constructor
:nodoc:.
- #inverse_of ⇒ Object
- #inverse_updates_counter_in_memory? ⇒ Boolean
-
#inverse_which_updates_counter_cache ⇒ Object
(also: #inverse_updates_counter_cache?)
We need to avoid the following situation:.
- #join_scope(table, foreign_table, foreign_klass) ⇒ Object
-
#join_scopes(table, predicate_builder = nil, klass = self.klass, record = nil) ⇒ Object
:nodoc:.
-
#klass_join_scope(table, predicate_builder = nil) ⇒ Object
:nodoc:.
-
#scopes ⇒ Object
Returns a list of scopes that should be applied for this Reflection object when querying the database.
- #strict_loading? ⇒ Boolean
- #strict_loading_violation_message(owner) ⇒ Object
- #table_name ⇒ Object
- #through_reflection? ⇒ Boolean
Constructor Details
#initialize ⇒ AbstractReflection
:nodoc:
164 165 166 167 168 169 170 |
# File 'lib/active_record/reflection.rb', line 164 def initialize @class_name = nil @counter_cache_column = nil @inverse_of = nil @inverse_which_updates_counter_cache_defined = false @inverse_which_updates_counter_cache = nil end |
Instance Method Details
#alias_candidate(name) ⇒ Object
328 329 330 |
# File 'lib/active_record/reflection.rb', line 328 def alias_candidate(name) "#{plural_name}_#{name}" end |
#build_association(attributes, &block) ⇒ Object
Returns a new, unsaved instance of the associated class. attributes
will be passed to the class’s constructor.
182 183 184 |
# File 'lib/active_record/reflection.rb', line 182 def build_association(attributes, &block) klass.new(attributes, &block) end |
#build_scope(table, predicate_builder = nil, klass = self.klass) ⇒ Object
336 337 338 |
# File 'lib/active_record/reflection.rb', line 336 def build_scope(table, predicate_builder = nil, klass = self.klass) Relation.create(klass, table:, predicate_builder:) end |
#chain ⇒ Object
332 333 334 |
# File 'lib/active_record/reflection.rb', line 332 def chain collect_join_chain end |
#check_validity_of_inverse! ⇒ Object
264 265 266 267 268 269 270 271 272 273 |
# File 'lib/active_record/reflection.rb', line 264 def check_validity_of_inverse! if !polymorphic? && has_inverse? if inverse_of.nil? raise InverseOfAssociationNotFoundError.new(self) end if inverse_of == self raise InverseOfAssociationRecursiveError.new(self) end end end |
#class_name ⇒ Object
Returns the class name for the macro.
composed_of :balance, class_name: 'Money'
returns 'Money'
has_many :clients
returns 'Client'
190 191 192 |
# File 'lib/active_record/reflection.rb', line 190 def class_name @class_name ||= -([:class_name] || derive_class_name).to_s end |
#constraints ⇒ Object
240 241 242 |
# File 'lib/active_record/reflection.rb', line 240 def constraints chain.flat_map(&:scopes) end |
#counter_cache_column ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/active_record/reflection.rb', line 244 def counter_cache_column @counter_cache_column ||= begin counter_cache = [:counter_cache] if belongs_to? if counter_cache counter_cache[:column] || -"#{active_record.name.demodulize.underscore.pluralize}_count" end else -((counter_cache && -counter_cache[:column]) || "#{name}_count") end end end |
#counter_must_be_updated_by_has_many? ⇒ Boolean
324 325 326 |
# File 'lib/active_record/reflection.rb', line 324 def counter_must_be_updated_by_has_many? !inverse_updates_counter_in_memory? && has_cached_counter? end |
#has_active_cached_counter? ⇒ Boolean
Returns whether this association has a counter cache and its column values were backfilled (and so it is used internally by methods like size
/any?
/etc).
315 316 317 318 319 320 321 322 |
# File 'lib/active_record/reflection.rb', line 315 def has_active_cached_counter? return false unless has_cached_counter? counter_cache = [:counter_cache] || (inverse_which_updates_counter_cache && inverse_which_updates_counter_cache.[:counter_cache]) counter_cache[:active] != false end |
#has_cached_counter? ⇒ Boolean
Returns whether this association has a counter cache.
The counter_cache option must be given on either the owner or inverse association, and the column must be present on the owner.
307 308 309 310 311 |
# File 'lib/active_record/reflection.rb', line 307 def has_cached_counter? [:counter_cache] || inverse_which_updates_counter_cache && inverse_which_updates_counter_cache.[:counter_cache] && active_record.has_attribute?(counter_cache_column) end |
#inverse_of ⇒ Object
258 259 260 261 262 |
# File 'lib/active_record/reflection.rb', line 258 def inverse_of return unless inverse_name @inverse_of ||= klass._reflect_on_association inverse_name end |
#inverse_updates_counter_in_memory? ⇒ Boolean
299 300 301 |
# File 'lib/active_record/reflection.rb', line 299 def inverse_updates_counter_in_memory? inverse_of && inverse_which_updates_counter_cache == inverse_of end |
#inverse_which_updates_counter_cache ⇒ Object Also known as: inverse_updates_counter_cache?
We need to avoid the following situation:
* An associated record is deleted via record.destroy
* Hence the callbacks run, and they find a belongs_to on the record with a
:counter_cache options which points back at our owner. So they update the
counter cache.
* In which case, we must make sure to *not* update the counter cache, or else
it will be decremented twice.
Hence this method.
285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/active_record/reflection.rb', line 285 def inverse_which_updates_counter_cache unless @inverse_which_updates_counter_cache_defined if counter_cache_column inverse_candidates = inverse_of ? [inverse_of] : klass.reflect_on_all_associations(:belongs_to) @inverse_which_updates_counter_cache = inverse_candidates.find do |inverse| inverse.counter_cache_column == counter_cache_column && (inverse.polymorphic? || inverse.klass == active_record) end end @inverse_which_updates_counter_cache_defined = true end @inverse_which_updates_counter_cache end |
#join_scope(table, foreign_table, foreign_klass) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/active_record/reflection.rb', line 200 def join_scope(table, foreign_table, foreign_klass) predicate_builder = klass.predicate_builder.with(TableMetadata.new(klass, table)) scope_chain_items = join_scopes(table, predicate_builder) klass_scope = klass_join_scope(table, predicate_builder) if type klass_scope.where!(type => foreign_klass.polymorphic_name) end scope_chain_items.inject(klass_scope, &:merge!) primary_key_column_names = Array(join_primary_key) foreign_key_column_names = Array(join_foreign_key) primary_foreign_key_pairs = primary_key_column_names.zip(foreign_key_column_names) primary_foreign_key_pairs.each do |primary_key_column_name, foreign_key_column_name| klass_scope.where!(table[primary_key_column_name].eq(foreign_table[foreign_key_column_name])) end if klass.finder_needs_type_condition? klass_scope.where!(klass.send(:type_condition, table)) end klass_scope end |
#join_scopes(table, predicate_builder = nil, klass = self.klass, record = nil) ⇒ Object
:nodoc:
227 228 229 230 231 232 233 |
# File 'lib/active_record/reflection.rb', line 227 def join_scopes(table, predicate_builder = nil, klass = self.klass, record = nil) # :nodoc: if scope [scope_for(build_scope(table, predicate_builder, klass), record)] else [] end end |
#klass_join_scope(table, predicate_builder = nil) ⇒ Object
:nodoc:
235 236 237 238 |
# File 'lib/active_record/reflection.rb', line 235 def klass_join_scope(table, predicate_builder = nil) # :nodoc: relation = build_scope(table, predicate_builder) klass.scope_for_association(relation) end |
#scopes ⇒ Object
Returns a list of scopes that should be applied for this Reflection object when querying the database.
196 197 198 |
# File 'lib/active_record/reflection.rb', line 196 def scopes scope ? [scope] : [] end |
#strict_loading? ⇒ Boolean
340 341 342 |
# File 'lib/active_record/reflection.rb', line 340 def strict_loading? [:strict_loading] end |
#strict_loading_violation_message(owner) ⇒ Object
344 345 346 347 348 |
# File 'lib/active_record/reflection.rb', line 344 def (owner) = +"`#{owner}` is marked for strict_loading." << " The #{polymorphic? ? "polymorphic association" : "#{klass} association"}" << " named `:#{name}` cannot be lazily loaded." end |
#table_name ⇒ Object
176 177 178 |
# File 'lib/active_record/reflection.rb', line 176 def table_name klass.table_name end |
#through_reflection? ⇒ Boolean
172 173 174 |
# File 'lib/active_record/reflection.rb', line 172 def through_reflection? false end |