Rails [已解决] Postgres 数据库错误?

joezhang · 2014年10月31日 · 最后由 joezhang 回复于 2014年10月31日 · 3295 次阅读

以下代码在开发环境sqlite3数据库运行正常,在Production环境Postgres9.3数据库却出现错误,是什么原因呢?请指教,谢谢!

class HomeController < ApplicationController
  def index
    @new_3_projects = Project.find(:all, limit:3)
    @staff_pick_project = Project.first
  end
end
App 18126 stdout: Processing by HomeController#index as HTML
App 18126 stdout: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "all"
App 18126 stdout: LINE 1: ...cts".* FROM "projects"  WHERE "projects"."id" IN ('all', '--...
App 18126 stdout:                                                              ^
App 18126 stdout: : SELECT "projects".* FROM "projects"  WHERE "projects"."id" IN ('all', '---
App 18126 stdout: :limit: 3
App 18126 stdout: ')  ORDER BY created_at DESC
App 18126 stdout: Completed 500 Internal Server Error in 1ms
App 18126 stdout:
App 18126 stdout: ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "all"
App 18126 stdout: LINE 1: ...cts".* FROM "projects"  WHERE "projects"."id" IN ('all', '--...
App 18126 stdout:                                                              ^
App 18126 stdout: : SELECT "projects".* FROM "projects"  WHERE "projects"."id" IN ('all', '---
App 18126 stdout: :limit: 3
App 18126 stdout: ')  ORDER BY created_at DESC):
App 18126 stdout:   app/controllers/home_controller.rb:3:in `index'

解决方案: rails 4 不推荐find了,推荐where

共收到 5 条回复

换成last就正常了,难道是运行在vagrant虚拟机上内存不足(512M)导致的问题?

class HomeController < ApplicationController
  def index
    @new_3_projects = Project.last(3)
    @staff_pick_project = Project.first
  end
end
2楼 已删除

App 18126 stdout: ActiveRecord::StatementInvalid (PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "all" App 18126 stdout: LINE 1: ...cts".* FROM "projects" WHERE "projects"."id" IN ('all', '--... 这报错信息还不够明显么.......

为什么不用to_sql看看生成的sql语句是啥呢? find 已经不推荐使用了. Project.all.limit(3)

#4楼 @itomato +1 rails 4 不推荐find了,推荐where

class HomeController < ApplicationController
  def index
    @new_3_projects = Project.all.last(3)
    @staff_pick_project = Project.first
  end
end

问题已解决,谢谢大家指点!

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册