I found out that using where with symbol :my_id => nil and using old school one with ? is different. Could anyone explain me why?
MyTable.where(\"my_id = ? \", n
The correct SQL syntax is my_id IS NULL
, so if you change your first snippet to the following it will work:
MyTable.where("my_id IS ?", nil).first
Both syntaxes are perfectly fine. It's up to your own preference. However, if it's a parameter and you don't know whether it will be nil
or not, you'd better use:
MyTable.where(:my_id => parameter).first