title: Rails - Strong parameters with empty arrays
body: <p>I'm sending an array of association ids, say <code>foo_ids</code> to my controller. To permit an array of values, I use:</p>
  
  <pre><code>params.permit(foo_ids: [])
  </code></pre>
  
  <p>Now, the problem is that if I send an empty array of <code>foo_ids</code>, the parameter is ignored. Instead of clearing all <code>foos</code> as an empty array should do, the association is left alone, because <code>foo_ids</code> isn't permitted.</p>
  
  <p>This may be because <a href="https://stackoverflow.com/questions/14647731/rails-converts-empty-arrays-into-nils-in-params-of-the-request">an empty array is converted to nil in rails</a>, and that nil value is ignored as strong parameters is looking for an array of scalar values, not a single scalar value.</p>
  
  <p>Can anyone suggest a good way to solve this? Thanks!</p>
  
  <p><strong>Additional info</strong></p>
  
  <p>In an update controller action, I need to be able to handle two cases. I need to be able to set <code>foo_ids</code> to an empty array. I also need to be able to ignore <code>foo_ids</code> if I merely want to update another field. Setting <code>foo_ids</code> to an empty array if nil does not work for this second case.</p>