【发布时间】:2015-02-03 16:25:31
【问题描述】:
我有一个这样的模型:
class Vote
belongs_to :content, counter_cache: true
end
class Content
has_many :votes
has_many :votes_up, -> { where(positive: true) }, class_name: 'Vote'
has_many :votes_down, -> { where(positive: false) }, class_name: 'Vote'
end
如果我创建了正确的迁移,当我执行mycontent.votes_count 时,它不会进行查询。
但是mycontent.votes_up.count 和mycontent.votes_down.count 呢?我可以以相同(简单)的方式缓存它们吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord database-performance