title: Ruby on Rails - Dynamic SQL Query
body: <p>I am executing the following raw sql query in one of my controllers :</p>
  
  <pre><code>active_users_query = &lt;&lt;-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) &gt; current_date - interval '30 days' 
         OR date_trunc('day', calendar_days.created_at) &gt; current_date - interval '30 days' 
         OR date_trunc('day',users.current_sign_in_at) &gt; current_date - interval '30 days') 
         AND subscriptions.code_id = 2  
  SQL
  </code></pre>
  
  <p>Is there a way I can add some RoR code to the last line of this query to generate the code_id dynamically ?</p>
  
  <p>Something like this :</p>
  
  <pre><code>AND subscriptions.code_id = '@subscription.code'
  </code></pre>