按照计数排序has_many:通过 [英] Order By Count on has_many :through

查看:182
本文介绍了按照计数排序has_many:通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails 4.我有一个应用程序,我有一个多对多的关系:

I use Rails 4. I have an application where i have a many-to-many relationship :

class User < ActiveRecord::Base
  has_many :relationshipfollows, :foreign_key => "follower_id",
                           :dependent => :destroy
  has_many :following, :through => :relationshipfollows, :source => :followed
end

class Project < ActiveRecord::Base
 has_many :relationshipfollows, :foreign_key => "followed_id",
                           :dependent => :destroy
 has_many :followers, :through => :relationshipfollows, :source => :follower
end

class Relationshipfollow < ActiveRecord::Base
  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "Project"
end

我遵循本教程: http://ruby.railstutorial.org/chapters/following-users?version=3.0#top

但现在我想列出所有由追随者数量排序的项目。像这样:

But now I'd like to list all projects ordered by the number of follower. Like this :

1. project1 | 99 followers
2. project2 | 16 followers
3. project3 | 2 followers
...

我是rails的新手,一个错误,因为我尝试了很多例子像这样:
Rails 3对has_many:through
has_many,通过:关系计数

I'm new to rails and I guess I keep making a mistake because I try a lot of examples like this : Rails 3 Order By Count on has_many :through or has_many , through: relationship count

我尝试这个方法:
Project.joins(:relationshipfollows).group .project_id)。order(count(relationshipfollows.project_id)desc)

但我有这个错误: SQLite3 :: SQLException:没有这样的列:relationshipfollows.project_id:SELECTprojects。* FROMprojectsINNER JOINrelationshipfollowsON关系关系。followed_id=projects。idGROUP BY relationshipfollows.project_id ORDER BY count(relationshipfollows.project_id)desc

我尝试另一种方法:

Project.joins(:relationshipfollow).select('following.*, COUNT(followers.id) AS user_count').group('project_id').order('COUNT(followers.id) DESC')

但我有这个错误:项目没有发现关系关系也许你拼错了吗?

有人可以帮我找到正确的方向如何使它一切正常吗?

Could someone please help me to find the right direction how to make it all work ?

亲切的

编辑:
我认为问题从这里。当我尝试这样:

Edit : I think the problem its from here. When I try this :

Relationshipfollow.select(:followed_id, "COUNT(follower_id) AS total").group(:followed_id).order("total DESC")

他返回我:


=>#ActiveRecord :: Relation [#Relationshipfolfol id:nil,followed_id:2,#Relationshipfollow id:nil,followed_id:1,
#Relationshipfollow id :nil,followed_id:3]

=> # ActiveRecord::Relation [# Relationshipfollow id: nil, followed_id: 2, # Relationshipfollow id: nil, followed_id: 1, # Relationshipfollow id: nil, followed_id: 3]

所有项目都按关注者数量排序,相对我的测试。但是当我加入到我的模型项目像这样:

All projects are ordered by the number of followers and all followed_id (projects) are in the good order in relative of my test. But when I join this to my model Project like this :

Project.joins(:relationshipfollows).select(:followed_id, "COUNT(follower_id) AS total").group(:followed_id).order("total DESC")

他返回一个项目列表,但是project_id为NULL:

he return me a list of projects but with project_id NULL :

=> #ActiveRecord::Relation [# Project id: nil, # Project id: nil, # Project id: nil]


推荐答案

我的救援解决方案是在我的项目模型中添加一个整数numberfollowers

My rescues solution was to add a integer numberfollowers in my Project model

但是使用另一个项目和rails指南 http://guides.rubyonrails.org/active_record_querying.html ),我终于找到了答案我的请求

But with another project and the rails guides (http://guides.rubyonrails.org/active_record_querying.html), I finally found the answer to my request

Project.joins(:relationshipfollows).select('projects.*, COUNT(followed_id) as user_count').group('projects.id').order('user_count DESC')

这篇关于按照计数排序has_many:通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆