Moh*_*ain 12 null activerecord ruby-on-rails-3
如何在rails活动记录中匹配null.
User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, NULL]).first
Run Code Online (Sandbox Code Playgroud)
要么
User.where(['id = ? and active = ? and activation_code = ?', params[:id], 0, nil]).first
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.
ace*_*des 25
诀窍是使用散列,在这种情况下,ActiveRecord将使用"IS NULL"正确生成nil值的SQL.
User.where({:id => params[:id], :active => 0, :activation_code => nil})
Run Code Online (Sandbox Code Playgroud)
Zab*_*bba 20
试试这样:
User.where(['id = ? and active = ? and activation_code IS NULL', params[:id], 0]).first
Run Code Online (Sandbox Code Playgroud)
在Rails 4中,您可以这样做:
User.where(id: params[:id], active: 0).where.not(activation_code: nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18943 次 |
| 最近记录: |