Activerecord group by count和user

编程入门 行业动态 更新时间:2024-10-28 18:27:57
Activerecord group by count和user_id(Activerecord group by count and user_id)

我有两个带有has_many关联的模型。

User :has_many foos Foo :belongs_to user

Foo.joins(:用户)

我希望在Foo上有一个组,它返回count和user_id。 意味着我想知道'user_id'有多少'foos'。

例如:

user_id foos_count ------------- 1 31 2 52

I have two models with has_many associations.

User :has_many foos Foo :belongs_to user

Foo.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).count

Here is the query to meet the need :

Foo.group(:user_id).count

更多推荐

本文发布于:2023-08-03 12:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:group   Activerecord   user   count

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!