1
考慮以下協會活動記錄兩級協會和團體通過數
公司
has_many: departments
部門
belongs_to: company
has_many: employees
員工
belongs_to: department
考慮下面的示例數據
公司
+------------+--------------+
| company_id | company_name |
+------------+--------------+
| 1 | Company A |
| 2 | Company B |
+------------+--------------+
部門
+---------------+------------+-----------------+
| department_id | company_id | department_name |
+---------------+------------+-----------------+
| 1 | 1 | Administration |
| 2 | 1 | Development |
| 3 | 1 | Designing |
+---------------+------------+-----------------+
僱員
+-------------+---------------+---------------+----------------+
| employee_id | department_id | employee_name | employee_grade |
+-------------+---------------+---------------+----------------+
| 1 | 1 | EmpA | grade1 |
| 2 | 1 | EmpB | grade1 |
| 3 | 2 | EmpC | grade1 |
| 4 | 2 | EmpD | grade2 |
+-------------+---------------+---------------+----------------+
在RoR中是否有單線程來查找屬於每個等級的公司的僱員人數?也就是說,如果COMPANY_ID等於1,則輸出如下
+--------+-------+
| grade | count |
+--------+-------+
| grade1 | 3 |
| grade2 | 1 |
+--------+-------+
我嘗試的一種方法是通過它來獲取部門公司,然後循環,取每個部門的等級分組的員工數量,並添加計數。有沒有其他方法?