按顺序排列has_many:通过

时间:2014-03-28 03:46:53

标签: sql ruby-on-rails ruby count has-many-through

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

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 Order By Count on has_many :throughhas_many , through: relationship 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?

有人可以帮我找到正确的方向,如何让它全部工作?

亲切

编辑: 我认为问题来自这里。当我尝试这个时:

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

他回复了我:

  

=&GT; #ActiveRecord :: Relation [#Reshifollow id:nil,follows_id:2,#Relationshipfollow id:nil,follows_id:1,    #Histoffollow id:nil,followed_id:3]

所有项目都按照关注者的数量排序,并且所有follow_id(项目)在我的测试中都处于良好的顺序。但是,当我将这个加入我的模型项目时,这样:

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

他给我一个项目列表,但项目名为NULL:

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

3 个答案:

答案 0 :(得分:1)

这就是我要做的事情:

#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>

答案 1 :(得分:0)

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")

答案 2 :(得分:-1)

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

但是通过另一个项目和导轨指南(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')