Search code examples
ruby-on-railsruby-on-rails-4strong-parametershstoreruby-on-rails-4.1

Allow an array of hashes with a dynamic hash (hstore) inside


I'm stuck with strong_parameters and this array of hashes with a dynamic hash (hstore) inside.

The structure is the following:

{ contact_sources: [
  { id: 1, filled_fields: { randomstuff: 'randomdata', dunno: 123 } },
  { id: 2, filled_fields: { blah: 'blabla', dunno: 9043 } }
] }

So, my main attempt is the following:

params.permit(contact_sources: [{:filled_fields => []}, 'id'])

Which doesn't return filled_fields. Any suggestion on how to deal with it?

Update 1: I have the following model:

class ContactSource < ActiveRecord::Base
  # Fields: id:integer, filled_fields:hstore
end

In my action, I'm submitting multiple records at once (mass update), so I have an array of contact_source, but actually they don't belong to anything, it's just a mass update.


Solution

  • Looks like it's not possible to do it with "plain" strong_parameters syntax. The only option you have is to actually, after filtering, re-add those values with a loop. I know it's terrible but it's the only way right now. I submitted a bug to Rails actually.