简体   繁体   English

Rails:ActiveRecord :: StatementInvalid调试(postgreSQL)

[英]Rails: ActiveRecord::StatementInvalid Debug (postgreSQL)

It's giving me a hard time to debug this. 这让我很难调试它。 It was working perfectly before I moved to PostgreSQL from SQLite3. 在我从SQLite3迁移到PostgreSQL之前,它运行良好。 What's strange to me is that there's another controller that has almost the same code as this, and that page works fine. 对我来说奇怪的是,还有另一个控制器具有与此几乎相同的代码,并且该页面运行良好。 Could anyone help me? 有人可以帮我吗?

Error: 错误:

ActiveRecord::StatementInvalid in Good_posts#index

Showing c:/ruby/mangfeel/app/views/good_posts/index.html.erb where line #3 raised:

PG::Error: ERROR:  syntax error at or near "desc"
LINE 1: ...  "good_posts".* FROM "good_posts"  ORDER BY like desc LIMIT...
                                                         ^
: SELECT  "good_posts".* FROM "good_posts"  ORDER BY like desc LIMIT 15 OFFSET 0

good_posts_controller that's causing the problem: 引起问题的good_posts_controller

def index
 @good_posts = GoodPost.paginate(:page => params[:page], order: 'like desc', per_page: 15)
 @good_client_ip = request.remote_ip.encode! 'utf-8'

 respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @good_posts }
 end
end

post_controller , which has similar code but works fine: post_controller ,具有类似的代码,但工作正常:

def index
 @posts = Post.paginate(:page => params[:page], order: 'created_at desc', per_page: 15)
 @client_ip = request.remote_ip.encode! 'utf-8'

 respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @posts }
 end
end

I appreciate all the help! 我感谢所有的帮助!

LIKE is an operator so you can't use it as a column name unless you double quote it: LIKE是运算符,因此除非将其双引号引起来,否则不能将其用作列名:

@good_posts = GoodPost.paginate(... order: '"like" desc' ...)

Whenever ActiveRecord generates a column name in some SQL it will quote the column name for you; 每当ActiveRecord在某些SQL中生成列名时,它将为您引用该列名。 so .where(:like => 11) will be sent to the database as where "like" = 11 and everyone is happy. 因此.where(:like => 11)将以where "like" = 11形式发送到数据库,每个人都很高兴。 When you write the column name in an SQL snippet (such as .where('"like" = ?', 11) ) you'll have to double quote it to distinguish it from the pattern matching operator. 当您在SQL代码段中编写列名(例如.where('"like" = ?', 11) )时,必须将其双引号与模式匹配运算符区分开。 You'll be happier if you rename the column to something (such as likes or number_of_likes ) that doesn't conflict with a keyword 如果将列重命名为与关键字不冲突的内容(例如likesnumber_of_likes ),则会更快乐

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveRecord :: StatementInvalid在ContactController Rails和Postgresql中 - ActiveRecord::StatementInvalid in ContactController rails & postgresql Rails:ActiveRecord :: StatementInvalid in - Rails: ActiveRecord::StatementInvalid in Rails PostgreSQL:列“ table_rows”不存在(ActiveRecord :: StatementInvalid) - Rails PostgreSQL: column “table_rows” does not exist (ActiveRecord::StatementInvalid) rails 4 ActiveRecord :: StatementInvalid - rails 4 ActiveRecord::StatementInvalid ActiveRecord::StatementInvalid 在 Rails 4.2.7.1 中 - ActiveRecord::StatementInvalid in Rails 4.2.7.1 在rails 3.2上的ActiveRecord :: StatementInvalid ruby - ActiveRecord::StatementInvalid ruby on rails 3.2 Heroku + Rails错误ActiveRecord :: StatementInvalid - Heroku + Rails Error ActiveRecord::StatementInvalid ActiveRecord :: StatementInvalid:Rails中的PG :: UndefinedColumn - ActiveRecord::StatementInvalid: PG::UndefinedColumn in Rails Ruby on Rails:ActiveRecord :: StatementInvalid无法强制转换ActiveRecord :: StatementInvalid - Ruby on Rails: ActiveRecord::StatementInvalid can't cast ActiveRecord::StatementInvalid 为什么我在使用Postgresql的rails应用程序中使用布尔字段时收到ActiveRecord :: StatementInvalid:PG :: SyntaxError:ERROR? - Why am I receiving an ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR when using a boolean field in a rails app with Postgresql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM