PG :: SyntaxError用于Rails应用程序 [英] PG::SyntaxError for rails application

查看:43
本文介绍了PG :: SyntaxError用于Rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的索引控制器中,我有一个查询来查找所有过去发生过的事件,其中事件发生的时间是什么时候

IN my index controller I have a query to find all past events which have happened where "when is the date the event has took place

@expired = Location.where('when <= ?', Date.today)

这是我的index.html.erb视图。

This is my index.html.erb view.

<% @expired.each do |location| %>
               <tr>
                <td>
                  <%= location.city %>                                                                                                                                                                                  
                </td>
                <td><%= location.when %>
                </td>
                <td><%= location.venue %></td>
                <td>
                  <%= location.start_time %> to <%= location.end_time %>
                </td>
                <td>General</td>
                <td><a href="http://smithridgehc.co.uk/" target="_blank">Smithridge Healthcare</a></td>
              </tr>
<% end %> 

运行服务器时,出现以下错误:

When I run the server I get this following error:

PG::SyntaxError: ERROR:  syntax error at or near "when"
LINE 1: SELECT "locations".* FROM "locations" WHERE (when <= '2015-0...

是由于格式错误的日期造成的吗?

Could it be due to the wrong formatted date?

推荐答案

何时是Postgre的保留字(请参见 doc )。您应该使用 作为列名时:

when is a reserved word for Postgre (see doc). You should escape it with "" in the SQL when using it as a column name:

@expired = Location.where('"when" <= ?', Date.today)

这篇关于PG :: SyntaxError用于Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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