Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Model find Where Not | Query where not

I have a user model with optional twitter integration.

I've added a 'username' attribute that is only filled when a user has completed twitter authentication.

I want to find all users with the integration. What is the best way to find all objects where an attribute is something, or where it is not nil?

like image 825
Blair Anderson Avatar asked Dec 22 '13 23:12

Blair Anderson


2 Answers

Rails4 / Rails 5:

User.where.not(username: nil)

Rails 3:

User.where("username <> nil")

I would love to see other responses

like image 100
Blair Anderson Avatar answered Nov 14 '22 07:11

Blair Anderson


you can also do just write the sql in the where statement:

User.where("username <> nil")
like image 24
Steven Wolf Avatar answered Nov 14 '22 05:11

Steven Wolf