[英]How to restrict a not empty field in ruby on rails active record where clause
I am very new to ruby and want to restrict records that have not empty :c
field values as follows.我对 ruby 很陌生,想限制没有空的记录
:c
字段值如下。
Foo.where(:a => "x", :b => "y", :c ?? "")
My problem is that I not know which operator ??
我的问题是我不知道是哪个运营商
??
I have to use for that or if an operator for that exists at all.我必须为此使用,或者是否存在用于该操作的运算符。
As I am writing the question I came accross the where chains
in the ruby on rails api documentation .在我写这个问题时,我遇到了ruby on rails api 文档中的
where chains
。 If I write the where clause as follows I get what I want.如果我按如下方式编写 where 子句,我会得到我想要的。
Foo.where(:a => "x", :b => "y").where.not(:c => "")
So far so good the where chain gives me what I want.到目前为止很好, where 链给了我我想要的。 But is there a way to use an operator
??
但是有没有办法使用运算符
??
to bring it all together in a simple where clause statement as in my first example?像我的第一个示例一样,将所有内容整合到一个简单的 where 子句语句中?
To have valid syntax, you have to use =>
in place of ??
要获得有效的语法,您必须使用
=>
代替??
. . That's because you are passing a hash to
where
method.那是因为您将散列传递给
where
方法。
As you mentioned, you can chain where
and where.not
.正如你提到的,你可以链接
where
和where.not
。 Another solution is to use one where
as below:另一种解决方案是使用一个
where
如下所示:
Foo.where("a = ? and b = ? and c != ''", 'x', 'y')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.