我正在我的一个控制器中执行以下原始 sql 查询:
active_users_query = <<-SQL
SELECT count(DISTINCT patients.id)
FROM public.patients, public.subscriptions, public.users, public.calendar_days
WHERE patients.user_id = users.id
AND patients.id = calendar_days.patient_id
AND subscriptions.user_id = patients.user_id
AND (date_trunc('day',patients.last_sync) > current_date - interval '30 days'
OR date_trunc('day', calendar_days.created_at) > current_date - interval '30 days'
OR date_trunc('day',users.current_sign_in_at) > current_date - interval '30 days')
AND subscriptions.code_id = 2
SQL
有没有办法可以在此查询的最后一行添加一些 RoR 代码以动态生成 code_id ?
像这样的东西:
AND subscriptions.code_id = '@subscription.code'