如何在轨道活动记录中匹配空值。如何比较轨道活动记录查询中的空值
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