" /> " /> "/>

How to permit a array that can contain both a String and an Object with strong params?

101 views Asked by At

Is easy to permit an array of scalar elements:

ActionController::Parameters.new(
  schedule: ['2022-07-04']
).permit(schedule: []).to_h
=> {"schedule"=>["2022-07-04"]}

And also an array of objects:

ActionController::Parameters.new(
  schedule: [{ start: '2022-12-24', end: '2022-12-31' }]
).permit(schedule: [:start, :end]).to_h
=> {"schedule"=>[{"start"=>"2022-12-24", "end"=>"2022-12-31"}]}

But I've been struggling for some time to find a structure that does both. Was expecting something in the lines of

ActionController::Parameters.new(
  schedule: ['2022-07-04', { start: '2022-12-24', end: '2022-12-31' }]
).permit(schedule: [[:start, :end], []]).to_h
=> {"schedule"=>[{"start"=>"2022-12-24", "end"=>"2022-12-31"}]}

to work but no luck so far.

Of course I can write a multi-line solution, but honestly at this point I am puzzled!

0

There are 0 answers