如何在軌道活動記錄中匹配空值。如何比較軌道活動記錄查詢中的空值
User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, NULL]).first
或
User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, nil]).first
兩者都不能正常工作。
如何在軌道活動記錄中匹配空值。如何比較軌道活動記錄查詢中的空值
User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, NULL]).first
或
User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, nil]).first
兩者都不能正常工作。
試試這樣說:
User.where(['id = ? and active = ? and activation_code IS NULL', params[:id], 0]).first
訣竅是使用散,在這種情況下的ActiveRecord將正確生成零值SQL,使用 「IS NULL」。
User.where({:id => params[:id], :active => 0, :activation_code => nil})
在Rails 4,你可以這樣做:
User.where(id: params[:id], active: 0).where.not(activation_code: nil)
您正在使用什麼數據庫? – tybro0103 2010-12-02 22:19:10
什麼是「哪裏」的方法?從未見過它。 – tybro0103 2010-12-02 22:20:48
另外,你有什麼錯誤? – tybro0103 2010-12-02 22:21:40