I want to write a query which combines two scopes with an "OR" query (using Rails 4 though the issue is still there with rails 5).
Model1
scope :association_has_email, -> { joins(:model2).where.not(model2s:{email:nil}) }
scope :description_has_email, -> { where("description ~* ?", email_regex) }
(where email_regex is a regular expression picking out an email).
Which gives us SQL like:
SELECT \"model1s\".* FROM \"model1s\" WHERE (description ~* '[[:<:]][A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}[[:>:]]')
SELECT \"model1s\".* FROM \"model1s\" INNER JOIN \"model2s\" ON \"model2s\".\"id\" = \"model1s\".\"model2_id\" WHERE (\"model2s\".\"email\" IS NOT NULL)
Create a scope to pick out those opportunities where either the email is in the association or an embedded email in the text.
How do you write an "OR" query where one side needs the join and the other doesn't?