0

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

1 Answer 1

1

"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'})")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.