Shouldn't `where.not (field:" something ")` include `where (field: nil)`?

Maybe I'm losing my mind or just need a break, but in my console, rails Order.where(state: nil).countreturns 1010, but Order.where.not(state: "pending").countreturns 0... If the order status is nil, then this does not “wait”, so I expect the set returned not(state: "pending")will turn on the set where(state: nil).

Does it really not work? If not, does the other mode work?

EDIT: more info! When I move to another database, where some entries have a state other than nil, and I start Order.where.not(state: "pending").count, I return a bunch of orders, none of which are “waiting,” but none of them are equal to zero. It seems to where.notimplicitly add and not nilto the request?

EDIT: in desperation, I turned to the dark spirits.

# look into another shop, that has records
o = Order.where(shop_id: 2)

# summon dread spirits
t = Order.arel_table[:state]

o.where(t.eq(nil).or(t.eq("pending").not)).count

=> 1569

o.where(t.eq(nil)).count

=> 1471

So, in this case, I get 98 records whose status is not zero or “wait”, and I get all records whose status is zero. I would really like to know why I can’t just say where.not("pending")and have the same effect. If there may be an option that I can call? For example where.not("pending", include_nil: true)?

EDIT: by request in the comment @Filip Bartuzi

Order.where.not(state: "pending").to_sql

=> "SELECT \"orders\".* FROM \"orders\"  WHERE \"orders\".\"shop_id\" = 2 AND (\"orders\".\"state\" != 'pending')"

Orders.where(state: nil).to_sql

=> "SELECT \"orders\".* FROM \"orders\"  WHERE \"orders\".\"shop_id\" = 2 AND \"orders\".\"state\" IS NULL"
+4
source share
2 answers

Order.where.not(state: "pending").to_sql generates:

=> "SELECT \"orders\".* FROM \"orders\" WHERE \"orders\".\"shop_id\" = 2 AND (\"orders\".\"state\" != 'pending')"

It will return all records with VALUES that are not pending. When u sets pendingto nil(for example Order.first.update! state: nil, it assigns NULLto the database.

NULL is not interpreted as a value in SQL, so it will not be included in the response SELECT

, : where.not(field: "something") where(field: nil)!

, :

http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all

UPDATE Categories
SET CategoryName = NULL
WHERE CategoryName = 'Beverages'

, Count 8, NULL CategoryName

:

SELECT CategoryName FROM Categories
WHERE CategoryName != 'Condiments'

6 , ( NULL)

+5

SQL 3- , NULL , . 3- : (null )

p       |q      |p OR q |p AND q|p = q
True    |True   |True   |True   |True
True    |False  |True   |False  |False
True    |Unknow |True   |Unknown|Unknown
False   |True   |True   |False  |False
False   |False  |False  |False  |True
False   |Unknown|Unknown|False  |Unknown
Unknown |True   |True   |Unknown|Unknown
Unknown |False  |Unknown|False  |Unknown
Unknown |Unknown|Unknown|Unknown|Unknown

where (row.state == 'pending') , , row.state NULL, "Unknown". "" , .

Wikipedia.

+2

All Articles