Revision 6b4b489f
Added by Ori Rabin over 8 years ago
app/models/concerns/counter_cache_fix.rb | ||
---|---|---|
if key =~ /_id/
|
||
association = self.association(key.sub(/_id$/, '').to_sym)
|
||
if association.options[ :counter_cache ]
|
||
counter_name = self.class.name.underscore.split("/")[0].pluralize.to_sym
|
||
association.klass.reset_counters(old_value, counter_name) if old_value
|
||
association.klass.reset_counters(new_value, counter_name) if new_value
|
||
|
||
# in case of counter cache on STI, specify the :inverse_of option, otherwise, we might throw an error.
|
||
# i.e. VariableLookupKey belongs to puppetclass, but the association name on the puppetclass is lookup_keys, not puppetclass_lookup_keys
|
||
# thus, we define the correct name for the association to be derived.
|
||
if association.options[:inverse_of].is_a?(Symbol)
|
||
counter_name = association.options[:inverse_of]
|
||
else
|
||
counter_name = self.class.name.underscore.split("/")[0].pluralize.to_sym
|
||
end
|
||
|
||
association_name = counter_name.to_s.sub(/_count$/, "").to_sym
|
||
association.klass.reset_counters(old_value, association_name) if old_value
|
||
association.klass.reset_counters(new_value, association_name) if new_value
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #10832 - separating lookup keys into puppet and variable