我在postgres 8.4中安装了Rails 3.1。以下是我的gem版本:
activerecord (3.1.3) activemodel (= 3.1.3) activesupport (= 3.1.3) arel (~> 2.2.1) tzinfo (~> 0.3.29) activerecord适配器(1.2.1) activerecord-jdbcpostgresql适配器(1.2.1) activerecord-jdbcpostgresql适配器(~> 1.2.1) jdbc-postgres (~> 9.0.0)
现在,当我在控制器中执行此查询时:
@topics = Topic.find(:all, :conditions => ["\"ForumID\" in ?, @forum_ids]
我知道这个错误:
ActiveRecord::JDBCError: ERROR: syntax error at or near "'abc123'"
Position: 62: SELECT "topic".* FROM "topic" WHERE ("ForumID" in 'abc123','1234')
Completed 500 Internal Server Error in 314ms
ActiveRecord::StatementInvalid (ActiveRecord::JDBCError: ERROR: syntax error at or near "'abc123'"
Position: 62: SELECT "topic".* FROM "topic" WHERE ("ForumID" in 'abc123','1234')):我认为问题在于将括号放在SQL语句中的位置。它应该在“in”之后,而不是"ForumID“之前。
SELECT "topic".* FROM "topic" WHERE "ForumID" in ('abc123','1234')工作得很好,所以这是postgresql适配器中的一个bug,还是我在查询中做错了什么?
谢谢。
发布于 2011-12-09 15:59:24
看起来,您在查询中缺少括号。所以这个应该有效:
@topics = Topic.find(:all, :conditions => ["ForumID in (?)", @forum_ids])在Rails 3.1中,最好使用:
@topis = Topic.where("ForumID in (?)", @forum_ids)发布于 2011-12-09 16:03:45
你不是想要
@topics = Topic.find(:all, :conditions => ["\"ForumID\" in ?", @forum_ids]注意在这些条件下关闭的"。
另外,您可以在Rails 3中使用
@topics = Topic.where("\"ForumID\" in ?", @forum_ids)甚至(检查语法)
@topics = Topic.where(:ForumID => @forum_ids)https://stackoverflow.com/questions/8448034
复制相似问题