2017-03-07 34 views
0

我旁邊型號:ROR:計數器緩存,的has_many吼聲,嵌套PARAMS刪除

class Document < ActiveRecord::Base 
    has_many :sub_roles_documents, dependent: :destroy 
    has_many :sub_roles, through: :sub_roles_documents,class_name: '::SubRole' 
end 

class SubRole < ActiveRecord::Base 
    has_many :sub_roles_documents, dependent: :destroy 
    has_many :documents, through: :sub_roles_documents, class_name: '::Document' 
end 

class SubRolesDocument < ActiveRecord::Base 
    belongs_to :sub_role, counter_cache: :documents_count, touch: true 
    belongs_to :document, counter_cache: :sub_roles_count 
end 

當我刪除了使用嵌套參數計數器緩存sub_roles_count不改變一些文件sub_roles,但是當我添加新的子文件到文件都工作正常。 如果我直接刪除文件documents.sub_roles.delete(specific_sub_role)的子文件 - 它也很好。 對我來說最好的辦法是什麼?

回答

0

我想通了一個問題,所有在文件中寫道:

此選項可用於配置命名的自定義:counter_cache。當您在belongs_to關聯中自定義您的:counter_cache的名稱時,您只需要此選項。因爲我用的是自定義名稱計數器緩存

class Document < ActiveRecord::Base 
    has_many :sub_roles_documents, dependent: :destroy, counter_cache: :documents_count 
    has_many :sub_roles, through: :sub_roles_documents,class_name: '::SubRole' 
end 

在我來說,我必須寫未來。