开发者

Rails find_by_sql database type dependent

So in rails I use sqlite3 for development and mysql for production. Sqlite3 and mysql handle booleans differently ("t" or "f" in sqlite3 and true or false on mysql). Usually it's not a problem when I do a find because I can do this:

Comment.find(:all, :conditions => ["viewed = ?", false])

And rails will insert the appropriate value depending on environment. But what if I want to do a find_by_s开发者_运维技巧ql in which I need a boolean?


You're in luck! #find_by_sql also performs database-agnostic string replacement when you pass it an array.

Comment.find_by_sql(["SELECT * FROM comments WHERE viewed = ?",  false])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜