Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid, scope if a value is set?

I'm creating a scope in my rails app, I want my scope to return documents which have anything set for the field in question.

scope :address_available, where(:address => "")

So because some documents might not have that field defined, and others will have a string as the value. But how can I return the documents which are not nil?

like image 880
JP Silvashy Avatar asked Mar 27 '12 23:03

JP Silvashy


1 Answers

Use exists

scope :address_available, where(:address.exists => true)
like image 157
Kyle Avatar answered Nov 20 '22 07:11

Kyle