Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test for non-null in Ruby::Sequel?

Tags:

ruby

sequel

I'm looking to do something like

User.select(...).where(:name != nil)

without writing something like

User.select(...).to_a.find_all {|user| user.name}

I can select for null values, but not for non-null. Is there a trick, or outside Sequel's domain?

like image 275
Eric Avatar asked May 26 '14 21:05

Eric


People also ask

How do you check if something is null in Ruby?

You can check if an object is nil (null) by calling present? or blank? . @object. present? this will return false if the project is an empty string or nil .

Does null exist in Ruby?

Let's start out with “Nil” since it's the most common and easy-to-understand way of representing nothingness in Ruby. In terms of what it means, Nil is exactly the same thing as null in other languages. So, what is null? Null, as it exists in many languages, is a sort of value that represents “nothing”.


Video Answer


2 Answers

You can use exclude instead of where.

User.select(...).exclude(name: nil)
like image 131
Alex Peachey Avatar answered Oct 18 '22 10:10

Alex Peachey


You can do this:

User.select(...).where('name IS NOT NULL')
like image 33
zishe Avatar answered Oct 18 '22 09:10

zishe