我有两张桌子:
**Project**
id
description
outcome_id
**Outcome**
id
reference
description
我想计算由outcome_id分组的outcome_id的出现次数:
Project.group(:outcome_id).count(:outcome_id) #I think this is right
#returns {1=>1, 2=>3}
但是,我想做一个连接,所以我可以将outcome_id与结果的描述相关联,例如。
Outcome ID Description Count
1 foobar 1
2 barfoo 3
我如何实现这一目标?
这些是我的模特:
Outcome.rb
class Outcome < ActiveRecord::Base
has_many :projects
end
Project.rb
class Project < ActiveRecord::Base
belongs_to :outcome
end