【问题标题】:Order By Count on has_many :through在 has_many 上按计数排序:通过
【发布时间】:2014-05-07 09:11:30
【问题描述】:

我使用 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")

他还给我这个:

=> #ActiveRecord::Relation [#Relationshipfollow id:nil,followed_id:2,#Relationshipfollow id:nil,followed_id:1, # 关系follow id:nil,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]

【问题讨论】:

  • 您真正想对查询做什么以及 activeRecord 方法的结果是什么??
  • 为了更清楚,我编辑了我的帖子。我想找到一个查询以返回按关注者数量排序的项目列表。我尝试了 2 种不同的方法(查询),但没有一种工作。也许是因为我犯了一个错误。
  • 试试这个Project.joins(:relationshipfollows).select('following.*, COUNT(followers.id) AS user_count').group('project_id').order('COUNT(followers.id) DESC') 让我知道结果
  • 当我对我的变量执行 .each 时,我认为出现此错误:ActiveRecord::StatementInvalid SQLite3::SQLException: no such table: following: SELECT following.*, COUNT(followers.id) AS user_count FROM "projects" INNER JOIN "relationshipfollows" ON "relationshipfollows"."followed_id" = "projects"."id" GROUP BY project_id ORDER BY COUNT(followers.id) DESC'
  • 使用这个:它应该可以解决你的问题:Project.joins(:relationshipfollows).select('followers.*, COUNT(followers.id) AS user_count').group('project_id').order('COUNT(followers.id) DESC')

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


【解决方案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>

【讨论】:

  • 您好 Rich Peck,感谢您的帮助。用你的方法我有这个错误:ActiveRecord::StatementInvalid in Categories#show SQLite3::SQLException: no such table: followers: SELECT followers.*, COUNT(followers.id) AS follower_count FROM "projects" INNER JOIN "relationshipfollows" ON "relationshipfollows"."followed_id" = "projects"."id" INNER JOIN "users" ON "users"."id" = "relationshipfollows"."follower_id" GROUP BY project_id ORDER BY follower_count DESC 在这一行:&lt;% @mostfollow.each_with_index do |project, i| %&gt; 我真的不知道发生了什么,我的错误在哪里。
  • 我编辑了我的帖子。也许我的测试会给你更多关于我的错误的信息。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我的救援解决方案是在我的项目模型中添加一个整数 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')
    

    【讨论】:

    • 这不包括没有关注者的项目。
    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多