I’ve got a SQL query in Postgres that works fine from a SQL console/tool, but not through Rails’ ActiveRecord (ActiveRecord::Base.connection.execute/select_all). I’ve tried a bunch of stuff like escaping quotes, calling ActiveRecord::Base.quote/sanitize to no avail – ActiveRecord returns an empty set where I’ve verified a tuple gets returned with this query.
SELECT
... blah blah
FROM
... joins joins joins
inner join core.pat_assignments assignment on assignment.correspondent_alias_id = out_alias.id
inner join core.pats patent on patent.id = assignment.pat_id and (select regexp_matches(patent.us_class_current, '(\w+)\/')) = '{D02}'
where
in_alias.id in (1987, 5004)
The funny thing is, it returns something if I take out the last inner join line, specifically the regex match. So there’s something with:
(select regexp_matches(patent.us_class_current, '(\w+)\/')) = '{D02}'
that’s making it throw up, but I just can’t figure out why… any suggestions would be greatly appreciated!
You need to double that
\to get\wdown to the regex engine and then you have to double each of those to get them past Ruby’s string literal handling. And you should useE''to avoid a warning. Also, you don’t need that extra SELECT, you can compare theregexp_matchesreturn value directly. So, something like this should work:There’s no need to escape a slash in a PostgreSQL regex so I took that out too. Embedding a language (regex) inside a language (PostgreSQL’s SQL) inside another language (Ruby) tends to get a bit messy when they all want to use the same escape character.
For example, in
psqlthese things happen:And then from the Rails console: