范围导致Postgres错误

时间:2013-08-29 12:04:17

标签: ruby-on-rails postgresql

以下应用程序可以正常使用sqlite

书籍型号

class Book < ActiveRecord::Base
    attr_accessible :month, :pages, :title, :year, :author_attributes, :publisher_attributes

    belongs_to :author
    belongs_to :publisher

    accepts_nested_attributes_for :author
    accepts_nested_attributes_for :publisher
end

书籍控制器

class BooksController < ApplicationController
     respond_to(:html)

     expose(:books, :ancestor => [:author, :publisher]) { Book.scoped.paginate(:page => params[:page], :per_page=>params[:per_page])}
     expose(:book)
     # other functions here
end

使用postgresql,当我尝试访问http://localhost:3000/books/1

时出现以下错误
ActiveRecord::StatementInvalid in Books#show

Showing /Users/tunwinnaing/Projects/rails_projects/books/app/views/books/show.html.haml where line #1 raised:

PG::UndefinedParameter: ERROR:  there is no parameter $1    
LINE 1: SELECT COUNT(*) FROM "books"  WHERE "books"."id" = $1    
                                                           ^
: SELECT COUNT(*) FROM "books"  WHERE "books"."id" = $1
Extracted source (around line #1):

1: - provide( :title, "#{book.title} | " )    
2: %h1= book.title    
3: %hr    
4: .row

背景资料

  • ruby​​ 1.9.3
  • rails 3.2.13
  • decent_exposure 2.2.0
  • will_paginate 3.0.3
  • pg 0.16.0
  • postgresql 9.x(使用postgresapp)

尝试以下

  • What is causing PGError: ERROR: there is no parameter $1 in my Rails app - 我需要will_paginate并且无法移除页面
  • 如果我从Book.scoped.paginate删除expose(books),它可以正常工作,但我不知道如何在没有范围的情况下进行分页。
  • 我在prepared_statement: false中尝试了database.yml,并得出结论认为它与此错误无关。

问题的根源在哪里?我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

使用Kaminari,错误不再存在,我得到了我想要的功能。

深入了解pgwill_paginate的内部工作可能会找到问题的原因。