Tuesday, January 11, 2011

Rails arel/activerelation subquery gotcha



subsql = s.where(s[:name].matches("%#{site}%")).project(s[:id])

Incident.where(i[:supportable_type].eq('Site').and(i[:supportable_id].in([subsql]))).to_sql

=> "SELECT \"tickets\".* FROM \"tickets\" WHERE (\"tickets\".\"type\" = 'Incident') AND (\"tickets\".\"supportable_type\" = 'Site' AND \"tickets\".\"supportable_id\" IN (SELECT id FROM \"sites\" WHERE (\"sites\".\"name\" ILIKE '%Site A%')))"

No comments: