0

I am trying to fetch records on database on localhost.

My columns from & to are dateranges and I would like to convert daterange to date to find records. date = Date.parse(2017-02-17) sc = schedules.where("from <= ? AND to >= ?",date,date) gives error;

SELECT "schedule_days".* FROM "schedule_days" WHERE "schedule_days"."car_id" = ? AND (from <= '2017-02-17' AND to >= '2017-02-17') [["car_id", 2]] SQLite3::SQLException: near "from": syntax error: SELECT "schedule_days".* FROM "schedule_days" WHERE "schedule_days"."car_id" = ? AND (from <= '2017-02-17' AND to >= '2017-02-17') ActiveRecord::StatementInvalid: SQLite3::SQLException: near "from": syntax error: SELECT "schedule_days".* FROM "schedule_days" WHERE "schedule_days"."car_id" = ? AND (from <= '2017-02-17' AND to >= '2017-02-17')

Is this problem because from & to are datetime?. I also tried;

schedules.where("date(from) <= ? AND date(to) >= ?",date,date) no luck. I will then push this code to heroku postgresql

Shalafister's
  • 761
  • 1
  • 7
  • 34

1 Answers1

1

FROM is a reserved word in SQL. It could be that the parser does not realize that you are referring to a column in a table and not using the FROM keyword.

Did you already try using a different variable name, or an alias?

Mary
  • 101
  • 5
  • Changing the column name solved the problem http://stackoverflow.com/questions/1992019/how-can-i-rename-a-database-column-in-a-ruby-on-rails-migration – Shalafister's Feb 18 '17 at 01:01