Module: Mongoid::Association::Referenced::CounterCache

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Association
Defined in:
build/mongoid-7.0/lib/mongoid/association/referenced/counter_cache.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_callbacks!(association) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add the callbacks responsible for update the counter cache field.

Examples:

Add the touchable.

Mongoid::Association::Referenced::CounterCache.define_callbacks!(association)

Parameters:

Returns:

  • (Class)

    The association’s owning class.

Since:

  • 7.0



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/counter_cache.rb', line 105

def self.define_callbacks!(association)
  name = association.name
  cache_column = association.counter_cache_column_name.to_sym

  association.inverse_class.tap do |klass|
    klass.after_update do
      if record = __send__(name)
        foreign_key = association.foreign_key

        if attribute_changed?(foreign_key)
          original, current = attribute_change(foreign_key)

          unless original.nil?
            record.class.with(persistence_context) do |_class|
              _class.decrement_counter(cache_column, original)
            end
          end

          unless current.nil?
            record[cache_column] = (record[cache_column] || 0) + 1
            record.class.with(record.persistence_context) do |_class|
              _class.increment_counter(cache_column, current) if record.persisted?
            end
          end
        end
      end
    end

    klass.after_create do
      if record = __send__(name)
        record[cache_column] = (record[cache_column] || 0) + 1

        if record.persisted?
          record.class.with(record.persistence_context) do |_class|
            _class.increment_counter(cache_column, record._id)
          end
          record.remove_change(cache_column)
        end
      end
    end

    klass.before_destroy do
      if record = __send__(name)
        record[cache_column] = (record[cache_column] || 0) - 1 unless record.frozen?

        if record.persisted?
          record.class.with(record.persistence_context) do |_class|
            _class.decrement_counter(cache_column, record._id)
          end
          record.remove_change(cache_column)
        end
      end
    end
  end
end

Instance Method Details

#reset_counters(*counters) ⇒ Object

Reset the given counter using the .count() query from the db. This method is usuful in case that a counter got corrupted, or a new counter was added to the collection.

Examples:

Reset the given counter cache

post.reset_counters(:comments)

Parameters:

  • counters (Symbol, Array)

    One or more counter caches to reset

Since:

  • 4.0.0



18
19
20
21
22
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/counter_cache.rb', line 18

def reset_counters(*counters)
  self.class.with(persistence_context) do |_class|
    _class.reset_counters(self, *counters)
  end
end