繁体   English   中英

在has_many上按计数排序:through

[英]Order By Count on has_many :through

我使用Rails4。我有一个与我有多对多关系的应用程序:

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

但是现在,我想按关注者的数量列出所有已订购的项目。 像这样 :

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

我是Rails的新手,我想我一直在犯一个错误,因为我尝试了很多这样的示例: Rails 3 has_many:throughhas_many 上的Count by Count ,通过:关系计数

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

但是我有这个错误: SQLite3::SQLException: no such column: relationshipfollows.project_id: SELECT "projects".* FROM "projects" INNER JOIN "relationshipfollows" ON "relationshipfollows"."followed_id" = "projects"."id" GROUP 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')

但是我有这个错误: Association named 'relationshipfollow' was not found on Project; perhaps you misspelled it? Association named 'relationshipfollow' was not found on Project; perhaps you misspelled it?

有人可以帮我找到正确的方向,如何使其全部正常工作吗?

亲切地

编辑:我认为问题是从这里开始。 当我尝试这个:

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

他把这个还给我:

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

所有项目都按照关注者的数量排序,并且相对于我的测试,所有followed_id(项目)的顺序都很好。 但是当我像这样将其加入到我的模型项目中时:

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

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

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

这就是我要做的:

#app/models/project.rb
Class Project < ActiveRecord::Base
     has_many :relationshipfollows, :foreign_key => "follower_id", :dependent => :destroy
     has_many :followers, :through => :relationshipfollows, :source => :follower

     scope :sort_by_followers, -> { joins(:followers).select("followers.*", "COUNT(followers.id) AS follower_count").group(:project_id).order("follower_count DESC") }
end

#app/controllers/projects_controller.rb
def index
    @projects = Project.sort_by_followers
end

#app/views/projects/index.html.erb
<ol>
    <% @projects.each_with_index do |project, i| %>
        <li><%= "Project#{i} | #{project.follower_count} followers" %></li>
    <% end %>
</ol>

Project.joins(:relationshipfollows)将进行内部联接。 因此,没有关注者的项目将不包括在结果集中。 您需要这样的东西:

Project.select("projects.*, COUNT(relationshipfollows.follower_id) as follower_count").joins("LEFT OUTER JOIN relationshipfollows ON relationshipfollows.followed_id = projects.id").group("projects.id").order("follower_count DESC")

我的救援方案是在我的Project模型中添加一个整数numberfollowers

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM