Getting ActiveRecords if NOT nil

I would like to be able to collect all the records in a table where user_id is not null. This is what I have, but it does not work (although I worked in a separate project):

named_scope :all_registered, :conditions => ["user_id != ?", nil]
+3
source share
3 answers

SQL has a specific statement for checking NULL: IS NULLandIS NOT NULL

named_scope :all_registered, :conditions => ["user_id IS NOT NULL"]
+8
source

This should work:

named_scope :all_registered, :conditions => "user_id IS NOT NULL"
+1
source

In Rails 4, you can use NOT conditions , see Active Request Query Interface

eg:

User.where.not(id: nil)

or as a scope:

scope :all_registered, -> { where.not(user_id: nil) }
0
source

Source: https://habr.com/ru/post/1748181/


All Articles