Scope Order by Count with Conditions Rails
问题 I have a model Category that has_many Pendencies . I would like to create a scope that order the categories by the amount of Pendencies that has active = true without excluding active = false . What I have so far is: scope :order_by_pendencies, -> { left_joins(:pendencies).group(:id).order('COUNT(pendencies.id) DESC')} This will order it by number of pendencies, but I want to order by pendencies that has active = true . Another try was: scope :order_by_pendencies, -> { left_joins(:pendencies)