title: How do I pass a variable into a prepared statement in Ruby?
body: <p>I have a method in Ruby to query a database and print out some data, and I'm trying to use a prepared statement instead.</p>
  
  <p>Here's the functioning method without the prepared statement:</p>
  
  <pre><code>def print_state_speakers(*states)
    puts "STATE SPEAKERS"
    state_string = "'#{states.*"', '"}'"
    state_speakers = $db.execute("
      SELECT name, location 
      FROM congress_members 
      WHERE location IN (#{state_string})
      ORDER BY location")
    state_speakers.each { |rep, location| puts "#{rep} - #{location}" }
  end
  </code></pre>
  
  <p>Here's my attempt at the same method using a prepared statement:</p>
  
  <pre><code>def print_state_speakers(*states)
    puts "STATE SPEAKERS"
    state_string = "'#{states.*"', '"}'"
    begin
      pst = $db.prepare "SELECT name, location 
      FROM congress_members 
      WHERE location IN (?)
      ORDER BY location"
      state_speakers = pst.execute state_string
    end
    state_speakers.each { |rep, location| puts "#{rep} - #{location}" }
  end
  </code></pre>
  
  <p>Here's where I call the method:</p>
  
  <pre><code> print_state_speakers('NJ', 'NY' , 'ME', 'FL', 'AK')
  </code></pre>
  
  <p>When I run the file with the 1st method it shows the data, when I use the 2nd, it shows nothing. It doesn't throw an error. I feel like the syntax needs to be different to account for the string being passed in, but I've been searching online and messing around with it for a while and can't get it to work.  Any insight as to how to fix the prepared statement would be appreciated.</p>