2011-11-09 4 views
0

Join 모델과 다형성을 가진 3 가지 모델 (Allen, Bob, Chris)이 있습니다. 및 조인 모델과 연결되는 사용자 모델.하나의 모델에서 여러 counter_cache를 트리거하는 방법 has_many : through

class Allen < ActiveRecord::Base 
    has_many :joins, :as => :resource 
    ... 
end 

class Bob < ActiveRecord::Base 
    has_many :joins, :as => :resource 
    ... 
end 

class Chris < ActiveRecord::Base 
    has_many :joins, :as => :resource 
    ... 
end 

class Join < ActiveRecord::Base 
    belongs_to :initiator, :class_name => "User", :foreign_key => "user_id" 
      :counter_cache => "How to write with 3 different counter cache?" 

    belongs_to :resource, :polymorphic => true, :counter_cache => :resources_count 
end 

class User < ActiveRecord::Base 
    has_many :joins 
    has_many :allens, :through => :joins, :source => :initiator 

    has_many :initial_joins, :class_name => "Join" 
end 

내 질문은 사용자 모델

에 밥, 크리스 알렌의 카운터 캐시를 작성하거나 여기를 검토 할 수있는 방법입니다 : 내가 생각 https://gist.github.com/1350922

답변

0

을 달성하기 위해 표준 방법이 없다 이. 콜백을 Allen, BobChris에 추가하면이 User과 관련된 모든 목록이 Bob과 관련되어 있으며 각각 수동으로 bobs_count을 다시 계산합니다.