For both basic conditions (like find statements) and the more general where clause, users have the ability to look for any of a group of conditions. For example:
User.find_by_name(["One", "Two", "Three"])
User.find_by_name_and_age(["One", "Two"],[1,2,3])
or
User.where(:bonus_id => [1,2,3])
Their is a slight inconsistance here, though. If you use
User.find_by_bonus_id([nil,1,2])
This will return uses with a bonus id of “nil”, as could be expected! However, both the “find_by_…and…” format and “where” format do NOT work this way.
User.where(:bonus_id => [nil,1])
Will return ONLY those Users with a bonus_id of 1.
User.where(:bonus_id => [nil]
will return nothing all!
User.where(:bonus_id => nil)
however, works fine.
As near as I can tell, the where clause (and find_by_and method) collapse their arrays, removing any values that are non-truthy. This is a pretty significant problem for me.
Does anyone know a way to include nils in a where clause array (or a workaround), or am I going to have to end up joining multiple queries together in order to obtain the right behaviour?
Additional Notes:
– Version 3.0.11
– Nils are not actually being dropped, but rather under certain circumstances it is comparing IN (NULL) instead of IS NULL in the SQL. I’m leaving the title as is, since even though it is inaccurate, it’s how the problem appears at first when encountered.
I’m not able to reproduce your problem on Rails 3.1.0. What version are you using?
Try this:
Will return users where the bonus_id is either 1, 2, or null