모듈: Mongoid::Association::Referenced::CounterCache::ClassMethods

다음에 정의됨:
lib/mongoid/association/referenced/counter_cache.rb

인스턴스 메서드 요약 접기

인스턴스 메서드 세부 정보

#Decrement_counter(counter_name, ID) ⇒ 객체

ID와 일치하는 항목에서 카운터 이름을 하나씩 줄입니다. 이 메서드는 counter_cache가 활성화된 경우 연결 콜백에 사용됩니다.

예시:

댓글 카운터 감소

Post.decrement_counter(:comments_count, '50e0edd97c71c17ea9000001')

매개변수:

  • counter_name (기호)

    카운터 캐시 이름

  • id (string)

    카운터를 감소시킬 객체 의 ID입니다.



84
85
86
# 파일 'lib/mongoid/association/referenced/counter_cache.rb', 줄 84

def decrement_counter(counter_name, id)
  update_counters(id, counter_name.to_sym => -1)
end

#증분 _카운터 (counter_name, ID) ⇒ 객체

ID와 일치하는 항목에서 카운터 이름을 하나씩 증가시킵니다. 이 메서드는 counter_cache가 활성화된 경우 연결 콜백에 사용됩니다.

예시:

댓글 카운터 증가

Post.increment_counter(:comments_count, '50e0edd97c71c17ea9000001')

매개변수:

  • counter_name (기호)

    카운터 캐시 이름

  • id (string)

    카운터를 증가시킬 객체 의 ID입니다.



71
72
73
# 파일 'lib/mongoid/association/referenced/counter_cache.rb', 줄 71

def 증분_카운터(counter_name, id)
  update_counters(id, counter_name.to_sym => 1)
end

#reset_counters(ID, *counters) ⇒ 객체

.count() 메서드를 사용하여 지정된 카운터를 재설정합니다. db에서 쿼리합니다. 이 메서드는 카운터가 손상되었거나 새 카운터가 컬렉션에 추가된 경우에 유용합니다.

예시:

지정된 카운터 캐시 재설정

Post.reset_counters('50e0edd97c71c17ea9000001', :comments)

매개변수:

  • id (string)

    재설정할 객체의 ID입니다.

  • *counters (기호...)

    재설정할 하나 이상의 카운터 캐시입니다.



39
40
41
42
43
44
45
46
# 파일 'lib/mongoid/association/referenced/counter_cache.rb', 줄 39

def reset_counters(id, *카운터)
  문서 = id.is_a?(문서) ? id : 찾기(id)
  카운터. do |이름|
    Relation_association = 관계[이름]
    counter_name = Relation_association.inverse_association.counter_cache_column_name
    문서.update_attribute(counter_name, 문서.send(이름).카운트)
  end
end

#update_counters(ID, counters) ⇒ 객체

값 계수만큼 지정된 카운터를 업데이트합니다. 원자적 $inc 명령을 사용합니다.

예시:

댓글 카운터에 5 추가 및 좋아요에서 2 제거

counter.
Post.update_counters('50e0edd97c71c17ea9000001',
           :comments_count => 5, :likes_count => -2)

매개변수:

  • id (string)

    업데이트 할 객체 의 ID입니다.

  • 카운터 (해시)


58
59
60
# 파일 'lib/mongoid/association/referenced/counter_cache.rb', 줄 58

def update_counters(id, 카운터)
  위치(:_id => id).Inc(카운터)
end