我有两个表是像作为主模型元数据加入:如何选择计数的使用ActiveRecord
users
id
name
active
items
id
user_id
color
使用Rails,我想选择的活跃用户与项目的数量一起红色或蓝色。
喜欢的东西:
User.where(active: true).joins(:items).where(items: {color: ['red', 'blue']}).count(:items)
我想要的结果是当用户有项目的注释号码的用户的数组。
所以它最终可能像用户= ActiveRecord的查询,users.first.name == '约翰',users.first.items_count == 3
你会怎么做?
难道被列入? – Swards