Class: ActiveRecord::Reflection::AbstractReflection
- Inherits:
-
Object
- Object
- ActiveRecord::Reflection::AbstractReflection
show all
- Defined in:
- lib/active_record/reflection.rb
Overview
Defined Under Namespace
Classes: JoinKeys
Instance Method Summary
collapse
Instance Method Details
#alias_candidate(name) ⇒ Object
243
244
245
|
# File 'lib/active_record/reflection.rb', line 243
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.
151
152
153
|
# File 'lib/active_record/reflection.rb', line 151
def build_association(attributes, &block)
klass.new(attributes, &block)
end
|
#chain ⇒ Object
247
248
249
|
# File 'lib/active_record/reflection.rb', line 247
def chain
collect_join_chain
end
|
#check_validity_of_inverse! ⇒ Object
199
200
201
202
203
204
205
|
# File 'lib/active_record/reflection.rb', line 199
def check_validity_of_inverse!
unless polymorphic?
if has_inverse? && inverse_of.nil?
raise InverseOfAssociationNotFoundError.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'
167
168
169
|
# File 'lib/active_record/reflection.rb', line 167
def class_name
@class_name ||= (options[:class_name] || derive_class_name).to_s
end
|
#constraints ⇒ Object
177
178
179
|
# File 'lib/active_record/reflection.rb', line 177
def constraints
scope_chain.flatten
end
|
#counter_cache_column ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/active_record/reflection.rb', line 181
def counter_cache_column
if belongs_to?
if options[:counter_cache] == true
"#{active_record.name.demodulize.underscore.pluralize}_count"
elsif options[:counter_cache]
options[:counter_cache].to_s
end
else
options[:counter_cache] ? options[:counter_cache].to_s : "#{name}_count"
end
end
|
#counter_must_be_updated_by_has_many? ⇒ Boolean
239
240
241
|
# File 'lib/active_record/reflection.rb', line 239
def counter_must_be_updated_by_has_many?
!inverse_updates_counter_in_memory? && has_cached_counter?
end
|
#has_cached_counter? ⇒ Boolean
Returns whether a counter cache should be used for this association.
The counter_cache option must be given on either the owner or inverse association, and the column must be present on the owner.
233
234
235
236
237
|
# File 'lib/active_record/reflection.rb', line 233
def has_cached_counter?
options[:counter_cache] ||
inverse_which_updates_counter_cache && inverse_which_updates_counter_cache.options[:counter_cache] &&
!!active_record.columns_hash[counter_cache_column]
end
|
#inverse_of ⇒ Object
193
194
195
196
197
|
# File 'lib/active_record/reflection.rb', line 193
def inverse_of
return unless inverse_name
@inverse_of ||= klass._reflect_on_association inverse_name
end
|
#inverse_updates_counter_in_memory? ⇒ Boolean
225
226
227
|
# File 'lib/active_record/reflection.rb', line 225
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?
This shit is nasty. 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.
217
218
219
220
221
222
|
# File 'lib/active_record/reflection.rb', line 217
def inverse_which_updates_counter_cache
return @inverse_which_updates_counter_cache if defined?(@inverse_which_updates_counter_cache)
@inverse_which_updates_counter_cache = klass.reflect_on_all_associations(:belongs_to).find do |inverse|
inverse.counter_cache_column == counter_cache_column
end
end
|
#join_keys(association_klass) ⇒ Object
173
174
175
|
# File 'lib/active_record/reflection.rb', line 173
def join_keys(association_klass)
JoinKeys.new(foreign_key, active_record_primary_key)
end
|
#primary_key_type ⇒ Object
159
160
161
|
# File 'lib/active_record/reflection.rb', line 159
def primary_key_type
klass.type_for_attribute(klass.primary_key)
end
|
#quoted_table_name ⇒ Object
155
156
157
|
# File 'lib/active_record/reflection.rb', line 155
def quoted_table_name
klass.quoted_table_name
end
|
#table_name ⇒ Object
145
146
147
|
# File 'lib/active_record/reflection.rb', line 145
def table_name
klass.table_name
end
|
#through_reflection? ⇒ Boolean
141
142
143
|
# File 'lib/active_record/reflection.rb', line 141
def through_reflection?
false
end
|