Mark Locklear
Mark Locklear

Reputation: 5325

Rails where clause not equal to nil

Hey folks trying to do a simple query where I find all records where a particular attribute is nil. In this case...

irb(main):009:0> Registration.where(:registration_cancellation_token != nil)
  Registration Load (0.2ms)  SELECT `registrations`.* FROM `registrations` WHERE (1)
=> [#<Registration id: 1, orientation_id: 13, first_name: "mark", last_name: "Busta", email: "[email protected]", student_id: "3232333", phone: "3884448333", registration_cancellation_token: nil, registration_cancelled_at: nil, checked_in: false, created_at: "2014-02-20 13:46:31", updated_at: "2014-02-20 13:46:31">]

...but the query is returning all registrations. Any help appreciated...

Upvotes: 0

Views: 1324

Answers (2)

Leantraxxx
Leantraxxx

Reputation: 4596

Registration.where.not(registration_cancellation_token: nil)

This works for Rails 4

Upvotes: 4

Zakwan
Zakwan

Reputation: 1072

Registration.where("registration_cancellation_token IS NOT NULL")

Upvotes: 6

Related Questions