Why is Model.scoped generating invalid SQL?
When I run Book.scoped({:conditions => ['books.index LIKE ?','%query%']})
I get:
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "index": syntax error: SELECT * FROM "books" WHERE (books.index like '%query%')
What am I doing wro开发者_C百科ng?
Credit goes to Adam Bernier for pointing out that index
is a SQL keyword (cf. comment on question). I am reposting the solution here so that this question gets marked as answered.
While
Book.scoped({:conditions => ['`index` LIKE ?','%query%']})
works, for peace of mind, I decided to rename the column.
精彩评论