Search code examples
ruby-on-railsapistrong-parameters

Bulk API efficiency


I am currently working on a Rails (5 - Beta) app which is mainly a JSON API.


I want to accept/process the following JSON:

{
   cars: [
     {name: "Car 1", hp: 120, model: "Model D"},
     {name: "Car 2", hp: 80, model: "Model A"}
   ]
}

I am trying to take multiple instances of 'Car' with one call. (The idea is to avoid the HTTP overhead when multiple cars should be tranfered) With respects to Strong Parameters, how would I apply the require and permit methods here? I want to make sure that within the car instance only the specified attributes "survive".

Thanks a lot!


Solution

  • meanwhile I came up with the following solution/work around:

    single_cars = params.require(:cars)
    cars = Array.new
    cars.each { |c|
      cars.push c.permit(:name, :hp, :model)
    }
    

    And the cars array can then be used to create/update ActiveModel instances. What do you think? Does that make sense to you?