Activerecord group by count和user_id(Activerecord group by count and user_id)
我有两个带有has_many关联的模型。
User :has_many foos Foo :belongs_to userFoo.joins(:用户)
我希望在Foo上有一个组,它返回count和user_id。 意味着我想知道'user_id'有多少'foos'。
例如:
user_id foos_count ------------- 1 31 2 52I have two models with has_many associations.
User :has_many foos Foo :belongs_to userFoo.joins(:user)
I want to have a group by on Foo, where it returns with count and user_id. Means I want to know how many 'foos' with 'user_id'.
Eg:
user_id foos_count ------------- 1 31 2 52最满意答案
以下是满足需求的查询:
Foo.group(:user_id).countHere is the query to meet the need :
Foo.group(:user_id).count更多推荐
发布评论