【发布时间】:2020-11-22 00:21:35
【问题描述】:
我有两个模型,用户和部门,以及一个连接表 users_departments,以启用它们之间的 has_and_belongs_to_many 关联。我使用 PostgreSQL 作为数据库。
# users table columns
id
name
# departments table columns
id
name
# users_departments table columns
user_id
department_id
Rails 中计算 users_departments 表中记录总数的最佳方法是什么?最好不要创建新的模型类。
请注意,我不想计算特定用户或部门的记录(user.departments.count/departments.users.count),而是考虑所有用户和部门的表的记录总数。
【问题讨论】:
标签: ruby-on-rails activerecord