开发者

Rails find_by_sql database type dependent

开发者 https://www.devze.com 2023-01-18 21:44 出处:网络
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 wh

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

精彩评论

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

关注公众号