【发布时间】: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 :through 或has_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