来源:评论的has_many则返回0空计数
评论:的has_many training_comments
@negative_comments = Source.joins(:comments => [:training_comments]).where("training_comments.category_id = ? and comments.spam = ?", 2, false).select("sources.*, count(comments.id) as ncount").group("comments.source_id")
我想列出的来源与他们的负面评论数,但我失去它没有negative_comments(training_entries来源.category_id = 2)。我尝试了一切来实现这一点。我尝试了左连接,我试过ifnull但没有一个工作。任何帮助真的非常感激..
我想要做的
源计数
来源1 5
源2 0
source3 13
什么,我得到的是
源计数
来源1 5
source3 13
源表
id: integer
name: string
条
评论表
id: integer
source_id: integer
spam: boolean
Training_comments表
id: integer
comment_id: integer
category_id: integer
这将有助于了解您的数据库结构。 – feeela
我更新了问题 – rOrman