简体   繁体   English

如何仅查找已发布至少一条评论的用户

[英]How to find only the users who have posted at least one comment

I am using Rails 2.3.5 . 我正在使用Rails 2.3.5。

This is a standard case. 这是一个标准案例。 Tables are: users, comments, user_comments . 表是:用户,评论,user_comments。 I need to find all the users who have status 'active' and have posted at least one comment. 我需要找到状态为“有效”且已发布至少一条评论的所有用户。

I know comments table can have foreign key but this is a contrived example. 我知道评论表可以有外键,但这是一个人为的例子。 There are two users in the table. 表中有两个用户。 There are two comments. 有两条评论。 Both the comments are posted by the first user. 这两条评论都是由第一个用户发布的。

named_scope :with_atleast_one_comment, lambda { {
      :joins => 'inner join user_comments on users.id = user_comments.user_id ' } }


 named_scope :with_active_status, {:conditions => {:status => 'active'} }    

When I execute 当我执行

 User.with_atleast_one_comment.with_active_status 

I get two records. 我得到两条记录。 Since both the comments are posted by one user I want only one user. 由于两个评论都是由一个用户发布的,我只想要一个用户。

What's the fix? 有什么问题?

The with_at_least_one_comment scope isn't behaving as you expect it to. with_at_least_one_comment范围的行为与您期望的不同。 As it appears in the question, it will select a user for each entry in user_comments. 正如问题中所示,它将为user_comments中的每个条目选择一个用户。 Which results in the duplicate results you're seeing. 这导致您看到的重复结果。 When compounded with active_users, you will remove any records returned by with_at_least_one_comment that don't have active status. 当与active_users复合时,您将删除没有活动状态的with_at_least_one_comment返回的任何记录。

Let's start by simplifying the problematic named scope first. 让我们首先简化有问题的命名范围。 You don't need the lambda because there are no arguments to take, and the join can be outsourced to Active Record, which performs an inner join if given an association. 您不需要lambda,因为没有参数可以使用,并且连接可以外包给Active Record,如果给定关联,它将执行内部连接。

In short, this named scope will do exactly what you want. 简而言之,这个命名范围将完全符合您的要求。

named_scope :with_at_least_one_comment, :joins => :user_comments, 
  :group => 'users.id'

Specify the :uniq => true option to remove duplicates from the collection. 指定:uniq => true选项以从集合中删除重复项。 This is most useful in conjunction with the :through option. 这与:through选项一起使用时非常有用。

if i'm is not wrong, there is few way to achieve this... 如果我没有错,那么实现这一目标的方法很少......
unless User.comments? 除非User.comments?
or another way is also specify a new method in your controller and lastly... 或者另一种方法是在控制器中指定一种新方法,最后......
the info from Emfi should work have a try for it~ 来自Emfi的信息应该有用吗〜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PAPERCLIP:如何仅列出附加了图像的用户? - PAPERCLIP: How to list only users who have a image attached? Rails-向用户显示上个月发布过书籍的用户关注的用户 - Rails - Show the users that a user has followed who have posted a book in the last month Rails/SQL - 如何找到没有高级订阅的用户加入 - Rails/SQL - How to find users who does not have premium subscription with joins 对于未确认的用户,我该如何自动发送一次Devise确认电子邮件,但仅重发一次? - How can I resend a Devise confirmation email automatically, but only once, for users who have not confirmed? 查找通过ActiveRecord下了两个或更多订单的用户 - Find users who have made two or more orders with ActiveRecord 如何找到一个不仅是独生子而且是表亲的模特? - How does one find a model who is not only the only-child but only-cousin? 使用角色掩码查找具有特定角色的所有用户 - using a roles mask to find all users who have a certain role 收集在Rails中的Tag上发布了具有ActsasTaggable的项目的用户 - Collect users who posted items with ActsasTaggable on Tag in rails 3 如何显示所有喜欢我帖子的用户 - How to show all the Users who have liked my post 如何显示所有喜欢我的特定物品的用户 - How to show all the Users who have liked my specific Items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM