use*_*697 22 ruby-on-rails ruby-on-rails-3
我希望where子句具有相等且不相等的条件:
@user = User.where(:user_id => current_user.id, :author_id != current_user.id).nil? ? (render :something) : (render :somethingelse)
Run Code Online (Sandbox Code Playgroud)
以上不起作用:
语法错误,意外')',期待tASSOC ... d,:user_id!= current_user.id).nil??(渲染:索引):(重新...
但是,如果我将第二个条件!=改为,=>则它将起作用.
如何在一个clase中同时具备这两个条件?谢谢
Tyl*_*ick 23
以下是使用Arel生成查询" select * from users where user_id = ? and author_id != ?"的方法:
users = User.arel_table
User.where(users[:user_id]. eq(current_user.id).and(
users[:author_id].not_eq(current_user.id)))
Run Code Online (Sandbox Code Playgroud)
使用Arel并不像在简单条件下使用Hash条件那样简洁,但它更强大!
下面是该链接断言的完整列表(eq,not_eq,gt,lt,等)可用阿雷尔.
tar*_*aro 21
我相信,它应该是:
@user = User.where(['user_id = ? AND author_id <> ?', current_user.id, current_user.id])
render(@user ? :something : :somethingelse)
Run Code Online (Sandbox Code Playgroud)
bou*_*uby 18
Rails 4已经解决了这个问题
Model.where.not(:colname => nil)
#=> returns all records whose :colname values are not nil
Run Code Online (Sandbox Code Playgroud)
语法错误是由于您尝试使用!=而不是=>.该where方法不支持使用散列参数的不等式,因此需要使用数组参数来编写不相等的值.
User.where(:user_id => current_user.id).where(['users.author_id <> ?', current_user.id])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26576 次 |
| 最近记录: |