推向Heroku时出错 - ...出现在组中 - Ruby on Rails [英] Error when pushing to Heroku - ...appear in group - Ruby on Rails

查看:67
本文介绍了推向Heroku时出错 - ...出现在组中 - Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的第一个Rails应用程序部署到Heroku,并且似乎遇到了问题。在> git push heroku master heroku rake db:migrate 后出现错误消息:

I am trying to deploy my first rails app to Heroku and seem to be having a problem. After git push heroku master, and heroku rake db:migrate I get an error saying:

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

我已经在下面列出了完整的错误, PostControll#索引,因为它似乎是我在做分组的地方。最后我包含了我的routes.rb文件。我是ruby,rails和heroku的新手,对于简单/明显的问题感到抱歉。

I have included the full error below and also included the PostControll#index as it seems that is where I am doing the grouping. Lastly I included my routes.rb file. I am new to ruby, rails, and heroku so sorry for simple/obvious questions.

Processing PostsController#index (for 99.7.50.140 at 2010-04-21 12:50:47) [GET]

ActiveRecord::StatementInvalid (PGError: ERROR:  column "posts.id" must appear in the GROUP BY clause or be used in an aggregate function
: 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):
  vendor/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:82:in `send'
  vendor/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:82:in `paginate'
  vendor/gems/will_paginate-2.3.12/lib/will_paginate/collection.rb:87:in `create'
  vendor/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:76:in `paginate'
  app/controllers/posts_controller.rb:28:in `index'
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.0.1) lib/thin/connection.rb:80:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:78:in `catch'
  thin (1.0.1) lib/thin/connection.rb:78:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:57:in `process'
  thin (1.0.1) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run_machine'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run'
  thin (1.0.1) lib/thin/backends/base.rb:57:in `start'
  thin (1.0.1) lib/thin/server.rb:150:in `start'
  thin (1.0.1) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.0.1) lib/thin/runner.rb:173:in `send'
  thin (1.0.1) lib/thin/runner.rb:173:in `run_command'
  thin (1.0.1) lib/thin/runner.rb:139:in `run!'
  thin (1.0.1) bin/thin:6
  /usr/local/bin/thin:20:in `load'
  /usr/local/bin/thin:20

PostsController

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

路线.rb

ActionController::Routing::Routes.draw do |map|

  map.resources :ugtags
  map.resources :wysihat_files
  map.resources :users
  map.resources :votes
  map.resources :votes, :belongs_to => :user
  map.resources :tags, :belongs_to => :user
  map.resources :ugtags, :belongs_to => :user
  map.resources :posts, :collection => {:auto_complete_for_tag_tag_name => :get }
  map.resources :posts, :sessions
  map.resources :posts, :has_many => :comments
  map.resources :posts, :has_many => :tags
  map.resources :posts, :has_many => :ugtags
  map.resources :posts, :has_many => :votes
  map.resources :posts, :belongs_to => :user
  map.resources :tags, :collection => {:auto_complete_for_tag_tag_name => :get }
  map.resources :ugtags, :collection => {:auto_complete_for_ugtag_ugctag_name => :get }

  map.login 'login', :controller => 'sessions', :action => 'new'
  map.logout 'logout', :controller => 'sessions', :action => 'destroy'
  map.root :controller => "posts"

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

更新后显示模型和迁移

class Post < ActiveRecord::Base

  has_attached_file :photo
  validates_presence_of :body, :title
  has_many :comments, :dependent => :destroy
  has_many :tags, :dependent => :destroy
  has_many :ugtags, :dependent => :destroy
  has_many :votes, :dependent => :destroy
  belongs_to :user
  after_create :self_vote
      def self_vote
       # I am assuming you have a user_id field in `posts` and `votes` table.
       self.votes.create(:user => self.user)
      end

  cattr_reader :per_page 
    @@per_page = 10

end

迁移过帐

class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end

  def self.down
    drop_table :posts
  end
end

_



_

class AddUserIdToPost < ActiveRecord::Migration
  def self.up
    add_column :posts, :user_id, :string
  end

  def self.down
    remove_column :posts, :user_id
  end
end


推荐答案

问题是查询本身。

Postgresql在SQL查询中使用聚合函数有更严格的规则 - 您需要将列添加到您的group by子句中。

Postgresql has much stricter rules for the use of aggregate functions in SQL Queries - you need to add the columns to your group by clause.

这篇关于推向Heroku时出错 - ...出现在组中 - Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆