So I put quite a bit of time to write this query, and then found out the hard way that this is returning an array rather than an activerecord relation. DOH. This wouldn't be a problem, but i need to use Ransack on these results, which requires the relation.
So, basically, i need to convert this to Rails using the syntax .joins() and .select() but everything I have tried errors out. I am guessing I may need to dive into AREL?
OR, if this can be easily converted to an activerecord relation with minimal performance issues AND keeping my alias columns then that works too!
Any help or advice is appreciated on this one!
find_by_sql("
SELECT
subq.*,
renewal_date,
days_until_due,
renewal_stage_sort,
(
CASE
WHEN renewal_stage_sort IS NOT NULL THEN
CASE
WHEN days_until_due > 42 AND renewal_stage_sort >= 1 THEN TRUE
WHEN days_until_due > 28 AND days_until_due < 43 AND renewal_stage_sort >= 2 THEN TRUE
WHEN days_until_due > 13 AND days_until_due < 29 AND renewal_stage_sort >= 3 THEN TRUE
WHEN days_until_due > -1 AND days_until_due < 14 AND renewal_stage_sort >= 4 THEN TRUE
WHEN days_until_due < 0 AND renewal_stage_sort >= 5 THEN TRUE
ELSE FALSE
END
ELSE FALSE
END
)
AS on_target
FROM (
SELECT DISTINCT ON (renewals.id) renewals.*,
CASE
WHEN renewal_types.name = 'IEP-504' THEN patients.iep_renewal_date
WHEN renewal_types.name = 'RR' THEN patients.rr_date
ELSE NULL
END
AS renewal_date,
( CASE
WHEN renewal_types.name = 'IEP-504' THEN patients.iep_renewal_date::date
WHEN renewal_types.name = 'RR' THEN patients.rr_date::date
ELSE NULL
END
- current_date)
AS days_until_due,
renewal_stages.sort_order AS renewal_stage_sort
FROM renewals
INNER JOIN renewal_types ON renewal_types.id = renewals.renewal_type_id
LEFT JOIN renewal_stages ON renewal_stages.id = renewals.renewal_stage_id
INNER JOIN patients ON patients.id = renewals.patient_id AND patients.deleted_at IS NULL
WHERE renewals.deleted_at IS NULL
) subq
")