Rails area where not equal

I am writing a scope that should say select all calls where call_status = open and unit_id is not zero. I'm very weak at Ruby, and also new to Rails, and it's hard for me to express this right.

Here is what I have:

scope :open_calls, where(:call_status => "open", :unit_id != nil).order("id ASC")

Should I use another operator to compute nil?

+5
source share
2 answers

As far as I know, there is no way to tell ActiveRecord about creating a NULL clause with a hash. But you can link the string style and the hash style, where cluses:

scope :open_calls, where(:call_status => "open").where("unit_id IS NOT NULL").order("id ASC")
+8
source

You can do it:

scope :open_calls, where("call_status IS ? AND unit_id IS NOT ?", "open", nil).order("id ASC")

, "open" nil , ? . SQL- - / .. . . .

0

All Articles