我想使用Heroku,显然Postgresql比SQL对集合函数更严格。当我推到Heroku时,我收到一个错误,指出下面的内容。将表列添加到group by子句 - Ruby on Rails - Postgresql
On another question我问我收到了一些指导,说我应该将列添加到我的group by子句中,但我不知道该怎么做。请参阅下面的完整错误和PostsControll#索引。
SELECT posts.*, count(*) as vote_total FROM "posts" INNER JOIN "votes" ON votes.post_id = posts.id GROUP BY votes.post_id ORDER BY created_at DESC LIMIT 5 OFFSET 0):
PostsController
def index
@tag_counts = Tag.count(:group => :tag_name,
:order => 'count_all DESC', :limit => 20)
conditions, joins = {}, :votes
@ugtag_counts = Ugtag.count(:group => :ugctag_name,
:order => 'count_all DESC', :limit => 20)
conditions, joins = {}, :votes
@vote_counts = Vote.count(:group => :post_title,
:order => 'count_all DESC', :limit => 20)
conditions, joins = {}, :votes
unless(params[:tag_name] || "").empty?
conditions = ["tags.tag_name = ? ", params[:tag_name]]
joins = [:tags, :votes]
end
@posts=Post.paginate(
:select => "posts.*, count(*) as vote_total",
:joins => joins,
:conditions=> conditions,
:group => "votes.post_id",
:order => "created_at DESC",
:page => params[:page], :per_page => 5)
@popular_posts=Post.paginate(
:select => "posts.*, count(*) as vote_total",
:joins => joins,
:conditions=> conditions,
:group => "votes.post_id",
:order => "vote_total DESC",
:page => params[:page], :per_page => 3)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
format.atom
end
end
这样做。非常感谢。下一期将作为单独问题发布。 – bgadoci 2010-04-23 22:16:00
我一直使用一个简单的字符串来列出被分组的列。 – 2011-01-03 09:00:43