2011-08-06 22 views
13

次のファインダはIS NULLを出力します。ActiveRecord IS NOT NULL SQLファインダ

User.where(:id =>nil) #=> [] 
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL ORDER BY created_at DESC 

しかし、IS NOT NULLを印刷する方法が見つかりませんでしたか?

+1

重複http://stackoverflow.com/questions/4252349/rail-3-where-condition-using-not-null – Vache

+0

この質問はあなたの答えです - http://stackoverflow.com/questions/4252349/rail-3- where-condition-using-not-null – ipr101

答えて

15

最も簡単な、慣用のActiveRecordソリューション:

User.where.not(id: nil) 
+1

注: 'rails> = v4.0.2'でのみ動作します。参照:http://apidock.com/rails/ActiveRecord/QueryMethods/WhereChain/not – morgents

5

User.where.not(id: nil)は、古いバージョンのために働くレール

User.where("id IS NOT NULL") 4のために動作します。これは、あなたの仕事を行う必要があります

User.where("id IS NOT NULL")を試してみてください。

+0

これはhttp://stackoverflow.com/a/27613775/38765(これは最初に回答しました)とほぼ同じですが、Rails 3では動作しません。 –

0

Railsのあなたは、特定のIDを除外するために探している場合も、この

User.where("id is not NULL") 

を試してみて、意志以下nilの値を含めることができます4

仕事

User.where("id is NULL or id != 'excluded_id'")