Как подключить age_table к заказу? - PullRequest
0 голосов
/ 20 октября 2019

Контекст Я настраиваю приложение для отеля, где люди могут заказать номер. В форме заказа я хотел бы перебрать все age_tables отеля и назначить age_table extra_guest, чтобы пользователь мог ввести сумму для каждого из них. (например, взрослый 2, малыш 1).

-> Причина в том, что в комнате разные цены для каждой возрастной таблицы (см. Также параметры ниже). ,

К сожалению, я не могу правильно вставить его в базу данных.

  • Мой текущий код отправляет только 1 таблицу соединения вместо hotel.age_tables.count таблицы соединения
  • таблица соединений, которая отправлена, не связана с соответствующим extra_guest

Отношения

  • Отель has_many age_tables
  • В отеле есть_ многие номера_типов
  • тип номера имеет_множество номеров
  • В номере есть_ много дополнительных гостей
  • extra_guest принадлежит_ к возрастной таблице
  • заказ принадлежит к комнате
  • В заказе есть_ множество дополнительных_гестов (через объединяющуюся таблицу order_extra_ghest)

код

модели

class Order < ApplicationRecord
  belongs_to :hotel
  belongs_to :room

  has_many :order_extra_guests, inverse_of: :order, dependent: :destroy
  accepts_nested_attributes_for :order_extra_guests
  has_many :extra_guests, through: :order_extra_guests
end

class OrderExtraGuest < ApplicationRecord
  belongs_to :extra_guest
  belongs_to :order
  accepts_nested_attributes_for :extra_guest
end


class ExtraGuest < ApplicationRecord
  belongs_to :room_type
  belongs_to :age_table

  has_many :order_extra_guests
  has_many :orders, through: :order_extra_guests

  has_many :extra_guest_prices, dependent: :destroy, inverse_of: :extra_guest
  accepts_nested_attributes_for :extra_guest_prices, allow_destroy: true
end

class Hotel < ApplicationRecord
  has_many :room_types, dependent: :destroy
  has_many :orders, dependent: :destroy
  has_many :age_tables, dependent: :destroy
  accepts_nested_attributes_for :age_tables, allow_destroy: true
end

форма заказа

<%= simple_form_for [@hotel, @order] do |f|%>

<% @age_tables.each do |guest_type| %>
  <%= f.simple_fields_for :order_extra_guest do |extra_guest|%>
    <%= extra_guest.input :extra_guest_quantity ,label: false, collection: 1..99 %>
  <% end %>
<% end %>
<% end %>

Контроллер заказов

class OrdersController < ApplicationController

  def new
    @hotel = Hotel.find(params[:hotel_id])
    @order = Order.new
    @age_tables = @hotel.age_tables
    @hotel.age_tables.count.times{@order.order_extra_guests.build}
    @order.build_order_contact
    @room_type_list = @hotel.room_types
    authorize @order
  end

  def create
    @order = Order.new(order_params)
    @hotel = Hotel.find(params[:hotel_id])
    @order.hotel = @hotel
    authorize @order
    order.save
  end

   private
  def order_params
      params.require(:order).permit(:arrival, :departure, :payment,
        rooms_attributes: [:id,:name, :room_type_id,
          room_types_attributes: [:id, :name]],
      order_extra_guests_attributes: [:id, :extra_guest_id, :extra_guest_quantity, :_destroy,
        extra_guests_attributes: [:id, :name, :room_type_id, :age_table_id,
          room_types_attributes:[:id, :name]]])
  end
end

Параметры отправлены

{"utf8"=>"✓",
 "authenticity_token"=>"W1Pso85ynHGLbpwh63+8ymAlPtyau5bu7T3lqkOVFZpdXbnI7nP7vD9mDUR20HiNsTwZRdeGjS9RA==",
 "reservation"=>
  {"rooms"=>{"room_type"=>""},
   "reservation_extra_guest"=>{"extra_guest_quantity"=>"1"},
 "commit"=>"Save & proceed to additional options",
 "hotel_id"=>"109"}
...