'POST api request in two models in Ruby on rails

I would like users to be able to create a booking, and give it a partner (BookingPartner) in the same request, similar to this

"booking": {
        "address": "Paris, France",
        "client_id": "66",
        "budget_per_guest": "88",
        "guest_quantity": "35",
        "start_time": "022-04-18T00:00:00.000Z",
        "end_time": "022-04-18T00:00:00.000Z", 
        "partner" : "45"
    }

But in Ruby on Rails, I don't know how to structure the "create" method, that would work on both models.

My Booking model :

class Booking < ApplicationRecord
  belongs_to :client
  has_many :booking_partners, dependent: :destroy
end

My BookingPartner model :

class BookingPartner < ApplicationRecord
  belongs_to :booking
  belongs_to :partner
  validates :partner_id, uniqueness: { scope: :booking_id }
end

Any tips ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source