1

Simple query like this.

MyModel.where('student_id = :id AND from <= :date AND to >= :date', {:id => student.id, :date => day.to_s(:db)})

Returns an exception

=> ActiveRecord::JDBCError: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from <= '2017-02-28 00:00:00.000' AND to >= '2017-02-28 00:00:00.000')'

And I don't get why.

Viktor
  • 4,218
  • 4
  • 32
  • 63

1 Answers1

4

From is a reserved word you should use backtics when you have a column name from (better if you don't use column names based on reserved words)

MyModel.where('student_id = :id 
                  AND `from` <= :date 
                  AND `to` >= :date', {:id => student.id, :date => day.to_s(:db)})
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • 1
    I don't see a point in giving the answer to this question, since *identical* problems were solved here (SO) hundreds of times. Users like you (experienced + you have golded mysql badge) should rather close these questions as duplicates. – Andrey Deineko Mar 06 '17 at 08:30