开发者

How to compare null in rails active record query

How can I match null in rails active record.

User.where(['id = ? and active = ? and activatio开发者_Python百科n_code = ?', params[:id], 0, NULL]).first

or

 User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, nil]).first

Both are not working.


The trick is using a hash, in which case ActiveRecord will correctly generate SQL for nil values, using "IS NULL".

User.where({:id => params[:id], :active => 0, :activation_code => nil})


Try it like this:

User.where(['id = ? and active = ? and activation_code IS NULL', params[:id], 0]).first


In Rails 4 you can do this:

User.where(id: params[:id], active: 0).where.not(activation_code: nil)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜