简体   繁体   中英

How to get attributes from array of objects to create different model in Rails?

I have a Rails server running on the background used to capture API requests. Inside Rails, I have 2 associated models: schedule and worker . schedule has_many workers and worker belongs_to schedule

I am making a POST request from React (fetch method), this is what it looks like on React:

  return fetch(`api/schedules`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      date: date,
      message: message,
      user_id: 1,
      worker_info: {name: workerName, phone: phone}
    })

On Rails schedules_controller:

  def create
    @schedule = Schedule.new(schedule_params)
    @worker = @schedule.workers.new(worker_params)
    @worker.save
    if @schedule.save
      render json: @schedule
    else
      render json: @schedule, status: :unprocessable_entity
    end
  end

  ...

  def schedule_params
    params.require(:schedule).permit(:date, :message, :user_id)
  end

  def worker_params
    params.require(:worker_info).permit(:name, :phone)
  end

Note, the codes above works fine

I want to send to api/schedules several parameters: date, message, user_id , and some worker info ( name, phone ).

Schedule itself has attributes date, message, and user_id . Worker has attributes name and phone . My plan is to make only one request to Rails, stub some worker info, and let Rails create worker object inside Schedule. Again, I can do that with code above.

My question is, the complexity increases when I try to send, instead of a single object worker_info , an array of worker_infos .

worker_info: [{name: workerName, phone: phone}]

(In the future, I want to send a variable amount of workers, that's why I added the array. Right now, I just need to make it work with just one)

worker_info: [{name: 'Test Worker 1', phone: '12345'}, {name: 'Test Worker 2', phone: '54321'}, ...]

I cannot figure out how to extract worker information to make worker object if I add the array [] . Here is the rails server error:

Started POST "/api/schedules" for 127.0.0.1 at 2017-05-25 17:19:32 -0700
Processing by SchedulesController#create as */*
  Parameters: {"date"=>"2017-05-27T02:00:00.000Z", "message"=>"Mireya Hiya~!!", "user_id"=>1, "worker_info"=>[{"name"=>"Mireya How
e", "phone"=>"1234567890"}], "schedule"=>{"date"=>"2017-05-27T02:00:00.000Z", "user_id"=>1, "message"=>"Hello test!!"}}
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.0ms)



NoMethodError (undefined method `permit' for #<Array:0x007f8381fd2530>):

app/controllers/schedules_controller.rb:59:in `worker_params'
app/controllers/schedules_controller.rb:20:in `create'

I am avoiding Rails' accept_nested_attributes_for method because the way my other code is structured, it won't work and has been causing much grief.

How can I extract the information inside worker_info: [{name: workerName, phone: phone}] and create new Worker object with that?

Ho, man.

First of all, good on 'ya for avoiding accept_nested_attributes_for . What a cluster you-know-what.

So, the whole point of strong parameters is to be able to whitelist stuff when doing mass assignment. This doesn't really seem like a mass assignment situation, so I would abandon ship on the whole approach.

Instead, I would go bare knuckles codez. You know, something like:

app/services/api/schedules/workers_service.rb

class Api::Schedules::WorkersService

  class < self

    def call(params={})
      ...
      # do a bunch of clever stuff
      ...
    end

  end
end

And then in SchedulesController , do something like:

def create
  @schedule = Schedule.new(schedule_params)
  if @schedule.save
    Api::Schedules::WorkersService.call(
      schedule_id: @schedule.id,
      worker_info: params[:worker_info]
    )
    render json: @schedule
  else
    render json: @schedule, status: :unprocessable_entity
  end
end

You probably want to wrap some of that junk in a transaction in case things go South.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM