【发布时间】:2013-03-18 14:12:14
【问题描述】:
我有以下 2 个导轨型号:
class Profile < ActiveRecord::Base
belongs_to :user
has_many :votes, through: :user
default_scope includes(:user)
end
和
class Vote < ActiveRecord::Base
attr_accessible :by, :for
belongs_to :by, class_name: "User"
belongs_to :for, class_name: "User"
validates :by, :for, presence: true
validates_uniqueness_of(:by, scope: :for)
end
我正在尝试在个人资料上创建一个“顶级”范围,该范围根据相关用户记录收到的“赞成”票数对个人资料进行排序
投票是由用户为用户创建的。 “by”栏表示投票的用户,“for”栏表示收到投票的用户。我正在尝试获取获得最多选票的用户的个人资料。
这是我目前得到的:
scope :top,
select("profile.*, count(votes.id) AS votes_count").
joins(:votes, :user).
order("votes_count DESC")
这不适用于以下错误:
ActiveRecord::StatementInvalid: PG::Error: ERROR: column “votes_count”不存在
它也没有考虑“for”列
谁能帮帮我?
【问题讨论】:
标签: sql ruby-on-rails ruby-on-rails-3 postgresql activerecord