如果不是,则获取ActiveRecords

sco*_*ott 3 ruby null activerecord ruby-on-rails

我希望能够收集user_id不为null的表中的所有记录.这就是我所拥有的,但它似乎没有起作用(即使我已经在一个单独的项目中工作):

named_scope :all_registered, :conditions => ["user_id != ?", nil]
Run Code Online (Sandbox Code Playgroud)

Vin*_*ert 8

SQL有一个特定的运算符来检查NULL:IS NULLIS NOT NULL

named_scope :all_registered, :conditions => ["user_id IS NOT NULL"]
Run Code Online (Sandbox Code Playgroud)