I am trying to query my Database using raw SQL. I want to get objects with a like on a certain field. I also want to query these objects location relation for a certain field. This is the SQL I have so far:
Center.find_by_sql("SELECT * FROM Centers WHERE name ILIKE '%" + query + "%'
AND status = 1");
A center has a location relation and I want query the location field also. Location.country = something? The database I am using is a PostgreSQL db and I am using Datamapper as an ORM. I had this query setup using datamapper but the results were not good. Can anyone help me extend this query to include a query on the Center.location‘s country field?
You need a field in each table so SQL knows how to join the correct records. I am going to use .ID in the example but this needs to be changed to whatever field you may use (ID, SSN, TIN, ACCT, etc.) that can join the two tables.
You should swap out the .* for actual columns you need, like
SELECT Centers.name, Centers.status, Location.country