【问题标题】:get count of has_many association in rails query在rails查询中获取has_many关联的计数
【发布时间】:2015-06-04 09:51:28
【问题描述】:

我有两个modelPost 和“评论”。

Posthas_may comments.

查询: 我想获取哪个帖子中的最大 cmets 数。

这应该通过rail查询来完成。

【问题讨论】:

  • 你能添加评论谁down投票吗?

标签: mysql ruby-on-rails-3 model-associations


【解决方案1】:
@post = Post.
  joins(:comments).
  select('posts.*, count(comments.id) as comment_count').
  group("posts.id").
  first

【讨论】:

  • 我正在寻找一个查询,我可以从中获取在其他帖子中具有最大 cmets 数的帖子。现在你明白了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多