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.
o = Order.where(shop_id: 2)
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"
source
share