title: Query hstore fields where value exists and equals x
body: <p>I have a <code>Widgets</code> model, which has around 3 core active records. One column is named 'form_values' which has the format:</p>
  
  <pre><code>"{\"a\":1,\"b\":2}"
  {"a" =&gt; "1", "b" =&gt; "2", "product_type" =&gt; "1"}.to_json
  </code></pre>
  
  <p>I need to query all active records whose form_values contains key <code>a</code> and its value is 1.</p>
  
  <p>I had written something that didn't work, like this:</p>
  
  <pre><code>required_widgets = Widget.find_by_sql("select id from  (select id, form_values from widgets where form_values!= '' and form_values::jsonb?'' and form_values::json-&gt;&gt;'product_type' = '1') as required_widgets where (form_values::json-&gt;'a')::jsonb?'1' ")
  </code></pre>