epayne
epayne

Reputation: 149

PG::SyntaxError for rails application

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)

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?

Upvotes: 1

Views: 47

Answers (1)

Baldrick
Baldrick

Reputation: 24340

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)

Upvotes: 2

Related Questions