Rails where子句不等于nil

时间:2014-02-20 14:28:42

标签: ruby-on-rails ruby-on-rails-3

嘿大家试图做一个简单的查询,在那里我找到特定属性为零的所有记录。在这种情况下......

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: "marklocklear@gmail.com", 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">]

...但查询返回所有注册。任何帮助表示赞赏...

2 个答案:

答案 0 :(得分:5)

Registration.where("registration_cancellation_token IS NOT NULL")

答案 1 :(得分:4)

Registration.where.not(registration_cancellation_token: nil)

这适用于Rails 4