Spec-Zone.ru › Ruby on Rails 5.1

модуль ActiveRecord::CounterCache::ClassMethods

Публичные методы экземпляров

decrement_counter(counter_name, id, touch: nil) Показать исходный код
# File activerecord/lib/active_record/counter_cache.rb, line 163
def decrement_counter(counter_name, id, touch: nil)
  update_counters(id, counter_name => -1, touch: touch)
end

Уменьшает числовое поле на единицу с помощью прямого обновления SQL.

Этот метод работает так же, как increment_counter, но уменьшает значение столбца на 1 вместо увеличения.

Параметры

  • counter_name — имя поля, которое нужно уменьшить.

  • id — идентификатор объекта, который нужно уменьшить, или массив идентификаторов.

  • :touch — обновление временных меток столбцов при обновлении. Передайте true, чтобы обновить updated_at и/или updated_on. Передайте символ, чтобы обновить этот столбец, или массив символов, чтобы обновить только эти столбцы.

Примеры

# Decrement the posts_count column for the record with an id of 5
DiscussionBoard.decrement_counter(:posts_count, 5)

# Decrement the posts_count column for the record with an id of 5
# and update the updated_at value.
DiscussionBoard.decrement_counter(:posts_count, 5, touch: true)
increment_counter(counter_name, id, touch: nil) Показать исходный код
# File activerecord/lib/active_record/counter_cache.rb, line 138
def increment_counter(counter_name, id, touch: nil)
  update_counters(id, counter_name => 1, touch: touch)
end

Увеличивает числовое поле на единицу с помощью прямого обновления SQL.

Этот метод используется в основном для поддержания счётчиков counter_cache, которые используются для хранения агрегированных значений. Например, DiscussionBoard может кэшировать posts_count и comments_count, чтобы избежать выполнения запроса SQL для вычисления количества постов и комментариев каждый раз при отображении.

Параметры

  • counter_name — имя поля, которое нужно увеличить.

  • id — идентификатор объекта, который нужно увеличить, или массив идентификаторов.

  • :touch — обновление временных меток столбцов при обновлении. Передайте true, чтобы обновить updated_at и/или updated_on. Передайте символ, чтобы обновить этот столбец, или массив символов, чтобы обновить только эти столбцы.

Примеры

# Increment the posts_count column for the record with an id of 5
DiscussionBoard.increment_counter(:posts_count, 5)

# Increment the posts_count column for the record with an id of 5
# and update the updated_at value.
DiscussionBoard.increment_counter(:posts_count, 5, touch: true)
reset_counters(id, *counters, touch: nil) Показать исходный код
# File activerecord/lib/active_record/counter_cache.rb, line 27
def reset_counters(id, *counters, touch: nil)
  object = find(id)

  counters.each do |counter_association|
    has_many_association = _reflect_on_association(counter_association)
    unless has_many_association
      has_many = reflect_on_all_associations(:has_many)
      has_many_association = has_many.find { |association| association.counter_cache_column && association.counter_cache_column.to_sym == counter_association.to_sym }
      counter_association = has_many_association.plural_name if has_many_association
    end
    raise ArgumentError, "'#{name}' has no association called '#{counter_association}'" unless has_many_association

    if has_many_association.is_a? ActiveRecord::Reflection::ThroughReflection
      has_many_association = has_many_association.through_reflection
    end

    foreign_key  = has_many_association.foreign_key.to_s
    child_class  = has_many_association.klass
    reflection   = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
    counter_name = reflection.counter_cache_column

    updates = { counter_name.to_sym => object.send(counter_association).count(:all) }
    updates.merge!(touch_updates(touch)) if touch

    unscoped.where(primary_key => object.id).update_all(updates)
  end

  return true
end

Сбрасывает один или несколько счётчиков counter_cache до их правильного значения с помощью запроса SQL count. Это полезно при добавлении новых счётчиков counter_cache или если счётчик был повреждён или изменён напрямую с помощью SQL.

Параметры

  • id — идентификатор объекта, на котором вы хотите сбросить счётчик.

  • counters — один или несколько счётчиков ассоциаций для сброса. Может быть указано имя ассоциации или счётчика.

  • :touch — обновление временных меток столбцов при обновлении. Передайте true, чтобы обновить updated_at и/или updated_on. Передайте символ, чтобы обновить этот столбец, или массив символов, чтобы обновить только эти столбцы.

Примеры

# For the Post with id #1, reset the comments_count
Post.reset_counters(1, :comments)

# Like above, but also touch the +updated_at+ and/or +updated_on+
# attributes.
Post.reset_counters(1, :comments, touch: true)
update_counters(id, counters) Показать исходный код
# File activerecord/lib/active_record/counter_cache.rb, line 98
def update_counters(id, counters)
  touch = counters.delete(:touch)

  updates = counters.map do |counter_name, value|
    operator = value < 0 ? "-" : "+"
    quoted_column = connection.quote_column_name(counter_name)
    "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
  end

  if touch
    touch_updates = touch_updates(touch)
    updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
  end

  unscoped.where(primary_key => id).update_all updates.join(", ")
end

Общая реализация «обновления счётчиков», предназначенная в первую очередь для использования с increment_counter и decrement_counter, но которая также может быть полезна сама по себе. Она просто выполняет прямое обновление SQL для записи с заданным идентификатором, изменяя заданный набор счётчиков на заданную величину:

Параметры

  • id — идентификатор объекта, на котором вы хотите обновить счётчик, или массив идентификаторов.

  • counters — Хэш, содержащий имена полей для обновления в качестве ключей и величину для обновления поля в качестве значений.

  • :touch параметр — обновление временных меток столбцов при обновлении. Передайте true, чтобы обновить updated_at и/или updated_on. Передайте символ, чтобы обновить этот столбец, или массив символов, чтобы обновить только эти столбцы.

Примеры

# For the Post with id of 5, decrement the comment_count by 1, and
# increment the action_count by 1
Post.update_counters 5, comment_count: -1, action_count: 1
# Executes the following SQL:
# UPDATE posts
#    SET comment_count = COALESCE(comment_count, 0) - 1,
#        action_count = COALESCE(action_count, 0) + 1
#  WHERE id = 5

# For the Posts with id of 10 and 15, increment the comment_count by 1
Post.update_counters [10, 15], comment_count: 1
# Executes the following SQL:
# UPDATE posts
#    SET comment_count = COALESCE(comment_count, 0) + 1
#  WHERE id IN (10, 15)

# For the Posts with id of 10 and 15, increment the comment_count by 1
# and update the updated_at value for each counter.
Post.update_counters [10, 15], comment_count: 1, touch: true
# Executes the following SQL:
# UPDATE posts
#    SET comment_count = COALESCE(comment_count, 0) + 1,
#    `updated_at` = '2016-10-13T09:59:23-05:00'
#  WHERE id IN (10, 15)

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API