我通过创建一个带有连接表 DefectFound 的 TestPhase 模型解决了这个问题
test_phase.rb:
has_many :defect_founds
defect_found.rb:
belongs_to :test_phase
defect.rb:
belongs_to :defect_found, foreign_key: :found_in, primary_key: :name # defect_found.name = defect.found_in
has_one :test_phase, through: :defect_found
controller:
@defects = Defect.includes(:test_phase).select('found_in, COUNT(id) as backlog').group(:found_in).group_by(&:test_phase)
view:
%table
- @defects.each do |test_phase, backlogs|
%tr
%td= test_phase.name if test_phase.present?
%td= backlogs.map(&:backlog).inject(:+)