Reputation: 1428
I have this query
Person.where('email ~* ?', "(#{params['domains'][0]}|#{params['domains'][1]})")
The behavior I want is to return all the people whose email contains one or more of the domains. when my params = {"domains"=>['gmail.com']}
I get every single record instead of all the people that have gmail.com as part of their domain.
I tested this line
Person.where('email ~* ?', "(#{params['domains'][0]}|nil)")
and I got only the records with gmail.com
if there isn't a params['domains'][1]
then it should equal nil
. What's happening?
from looking at the SQL
SELECT COUNT(*) FROM "contacts" WHERE (full_name ~* '(jared|friedman)\s?\w?\s?(jared|friedman)') AND (email ~* '(gmail.com|)')
it seems the problem is with (gmail.com|)
. nil isn't being passed in
I could do a sort of hack by conditionally inserting false if domains has a size of one. But there must be a better way
params['domains'][1] = false if params['domains'].size < 2
Upvotes: 0
Views: 75
Reputation: 29599
"nil"
is not equal to "#{nil}"
. The second code evaluates to an empty string. If you want nil
as a string, change it to
"(#{params['domains'][0]}|#{params['domains'][1] || 'nil'})")
Upvotes: 1