为什么是语法错误?

Why is it a syntax error?

像这样的简单查询。

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

Returns异常

=> 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')'

我不明白为什么。

From 是一个保留字,当你有一个列名时你应该使用反引号from(如果你不使用基于保留字的列名更好)

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