2015-12-30 44 views
0

有人可以请帮助如何在此活动记录条件中使用'where'条件或为什么这会在rails 4.2中引发错误?在Ruby中的活动记录计算

p = Project.first 
    Project Load (34.6ms) SELECT `projects`.* FROM `projects` ORDER BY `projects`.`id` ASC LIMIT 1 
=> #<Project id: 1, name: "First Project", created_at: "2015-12-29 16:27:42", updated_at: "2015-12-29 16:27:42"> 

2.2.1 :031 > p.tasks.sum(:priority) 
    (26.8ms) SELECT SUM(`tasks`.`priority`) FROM `tasks` WHERE `tasks`.`project_id` = 1 
=> 9 

2.2.1 :032 > p.tasks.sum(:priority).where(:complete => 0) 
    (0.2ms) SELECT SUM(`tasks`.`priority`) FROM `tasks` WHERE `tasks`.`project_id` = 1 
**NoMethodError: undefined method `where' for 9:Fixnum** 

回答

1

sum回报Fixnum,没有AR关系。您需要反转wheresum订单:

p.tasks.where(:complete => 0).sum(:priority)