2013-02-04 5 views
2

몽고이 카운터 캐쉬를 사용하려고하는데 작동하지 않습니다. 몽고 이드 3.1.0 카운터 캐시가 작동하지 않습니다.

난 그냥

belongs_to :user, counter_cache: true 

을 사용하려고하지만 그럼

Problem: 
Invalid option :counter_cache provided to relation :user. 

Summary: 
Mongoid checks the options that are passed to the relation macros to ensure that no ill side effects occur by letting something slip by. 

Resolution: 
Valid options are: autobuild, autosave, dependent, foreign_key, index, polymorphic, touch, class_name, extend, inverse_class_name, inverse_of, name, relation, validate, make sure these are the ones you are using. 

를 반환 내가

include Mongoid::CounterCache 

내 웹 서버가 다시 노력을 다시 시작 추가,하지만

을 반환
uninitialized constant Mongoid::CounterCache 

이 문제에 대한 아이디어가 있습니까?

답변

3

나는이 똑같은 것을 만났다. 여기 나를 위해 일한 것이 있습니다.

이 클래스가 앱에 이미 있고 counter_cache를 나중에 추가하기로 결정했다고 가정 해 보겠습니다. 그래서 당신은

class User 
    include Mongoid::Document 
    field :name, type: String 
    has_many :things 
end 

class Thing 
    include Mongoid::Document 
    field :name, type: String 
    belongs_to :user, counter_cache: true 
end 

그런 다음 당신이 당신의 콘솔에 홉 이렇게 아이 클래스에 counter_cache: true을 추가 : 사람이 작업을 수행하는 쉬운 또는 청소기 방법이있는 경우

u = User.first 
u.things.count #=> 10 
u.things_count #=> NoMethodError: undefined method things_count 
User.update_counters(u.id, things_count: u.things.count) 
u.reload 
u.things_count #=> 10 

, 즉 좋지 않을까.

+1

작동합니다. 그러나 나는 새로운 아이를 시작할 때마다 이것을해야만합니까? 그것 없이는 작동하지 않는 것 같습니다. 아니면 부모님이 두 명이나 되는가? –