The with_at_least_one_comment area does not behave as you expected. As the question seems, he will select a user for each entry in user_comments. This leads to duplication of the results that you see. When added with active_users, you delete any entries returned with_at_least_one_comment that do not have an active state.
First, we’ll start by simplifying the problem area. You do not need a lambda, because there are no arguments, and the connection can be outsourced to an active record that performs an internal connection if an association is specified.
In short, this named scope will do exactly what you want.
named_scope :with_at_least_one_comment, :joins => :user_comments, :group => 'users.id'
source share