我在我的一个控制器中执行以下原始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'