像这样的简单查询。
MyModel.where('student_id = :id AND from <= :date AND to >= :date', {:id => student.id, :date => day.to_s(:db)})
返回异常
=> 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')'
我不明白为什么。
答案 0 :(得分:4)
From是一个保留字,如果您有一个列名from
,则应该使用backtic(如果您不使用基于保留字的列名,则更好)
MyModel.where('student_id = :id
AND `from` <= :date
AND `to` >= :date', {:id => student.id, :date => day.to_s(:db)})