I am trying to make next query with ActiveRecord and have no idea about how to achieve this SQL result without using find_by_sql call.
SELECT *
FROM table_x AS a,
(SELECT locator, max(day_parsed) AS max_day
FROM table_x
WHERE day_parsed BETWEEN params[:day_from] AND params[:day_to]
GROUP BY locator) AS b
WHERE a.locator = b.locator
AND a.day_parsed = b.max_day
Any idea?
find_by_sql? Why bother converting a readable SQL query to a non-readableActiveRecordcode?find_by_sqlreturns aHash. Ideally I would like to create an ActiveRecord model based in this result. I tried with a postgresql view, but because I need the date range params I discarded to use DB views. What is wrong is that I cannot use the active_record associations