我知道我可以使用反馈,但这应当是可行的。 我进行了长时间的搜寻,没有结果。 这是我所认为的工作。
def User < ActiveRecord::Base
has_many :documents
has_many :draft_docs , :class_name => Document , :conditions => { :status => draft }
has_many :published_docs , :class_name => Document , :conditions => { :status => published }
has_many :private_docs , :class_name => Document , :conditions => { :status => private }
end
def Document < ActiveRecord::Base
belongs_to :user , :counter_cache => true
belongs_to :user , :inverse_of => :draft_docs , :counter_cache => true
belongs_to :user , :inverse_of => :published_docs, :counter_cache => true
belongs_to :user , :inverse_of => :private_docs , :counter_cache => true
end
没有按计划开展工作,因为你可以看到其更新文件——面面面,而不是发表——笔记。
ruby-1.9.2-p180 :021 > User.reset_counters 2, :published_docs User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
(0.7ms) SELECT COUNT(*) FROM `documents` WHERE `documents`.`user_id` = 2 AND `documents`.`status` = published
(2.2ms) UPDATE `users` SET `documents_count` = 233 WHERE `users`.`id` = 2
=> true