简体   繁体   中英

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.

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 . If I write the where clause as follows I get what I want.

Foo.where(:a => "x", :b => "y").where.not(:c => "")

So far so good the where chain gives me what I want. 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?

To have valid syntax, you have to use => in place of ?? . That's because you are passing a hash to where method.

As you mentioned, you can chain where and where.not . Another solution is to use one where as below:

Foo.where("a = ? and b = ? and c != ''", 'x', 'y')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM