如何在ActiveRecord查询中包含association.count,以便它不执行第二个查询?

pix*_*rth 1 activerecord ruby-on-rails

在我的模型中,我经常有很多关联.我还经常需要显示有多少(即assoc.count).这会强制执行另一个查询,如下所示:

ruby-1.8.7-p334 :020 > Instructor.first.instructors_lessons.count
  Instructor Load (0.5ms)  SELECT `users`.* FROM `users` INNER JOIN `instructor_profiles` ON `instructor_profiles`.`instructor_id` = `users`.`id` LIMIT 1
  SQL (1.2ms)  SELECT COUNT(*) FROM `instructors_lessons` WHERE (`instructors_lessons`.instructor_id = 2817)
Run Code Online (Sandbox Code Playgroud)

当有一个或两个时,这很好,但是当有100个以上时,这个过程明显变慢.请看这个极其缓慢的过程(http://pastebin.com/p4Sj7q7s).我一直在使用缓存来处理一个非常慢的页面.

我可以把它放在查询中这样:但这是乏味的,有点挫败ActiveRecord关联的目的我觉得:

 select("instructors.*, count('instructors_lessons.instructor_id') as num_lessons").
Run Code Online (Sandbox Code Playgroud)

但后来我不能简单地说讲师.lessons.count ......

有没有办法使用连接()或包含(),以便不需要执行这个额外的COUNT()查询?

Gaz*_*ler 7

听起来你正在寻找一个柜台缓存.

您只需使用_count创建一个列,然后在您所属的关联中指定counter_cache.

在您的情况下,您将向教师列添加一个名为"lessons_count"的字段.然后执行以下操作:

belongs_to :instructor, :counter_cache => true
Run Code Online (Sandbox Code Playgroud)

当关联记录总数发生变化时,此值将自动更新总计.