如何在创建操作中创建 JOIN 表并分配相关 id

对于房间预订,我想包括房间选项。


选项及其连接表reservation_options 似乎正确插入到我的参数中,但我无法正确分配连接表的option_quantity 值。它给了我错误消息```没有将符号隐式转换为整数``


由于选项是在表单中动态生成的,因为它们取决于所选的 room_type,因此我尝试在创建操作中构建它们。但是,我无法遍历参数并构建它们(请参阅为我的尝试创建操作)。


参数


 Parameters: {"utf8"=>"✓", "authenticity_token"=>"7QJHgdYW8ubKiNpLPET7tYzGNyqmp69dafo8NDaAUBf2PL3CI9XhKmd/am2Mo/A93EFZQByltwe2e4wepMXdqw==", "reservation"=>{"rooms"=>{"room_type"=>"185"}, "arrival"=>"2019-10-25", "departure"=>"2019-10-26", "room_id"=>"266", "reservation_contact_attributes"=>{"first_name"=>"John", "last_name"=>"Doe", "street"=>"street", "street_number"=>"", "zipcode"=>"4049", "city"=>"Gent", "country"=>"", "email"=>"john@hotmail.com", "phone"=>""}, "payment"=>"not paid"}, "reservation_options_attributes"=>[{"option_id"=>"109", "option_quantity"=>"1"}, {"option_id"=>"110", "option_quantity"=>"1"}], "commit"=>"Save & proceed to additional options", "hotel_id"=>"109"}

楷模


class Reservation < ApplicationRecord

  has_many :reservation_options, dependent: :destroy

  has_many :options, through: :reservation_options

  accepts_nested_attributes_for :reservation_options

end


class ReservationOption < ApplicationRecord

  belongs_to :option

  belongs_to :reservation

  accepts_nested_attributes_for :option

end


class Option < ApplicationRecord

  belongs_to :room_type

  has_many :reservation_options, dependent: :destroy

  has_many :reservations, through: :reservation_options

  validates :name, presence: true

end

保留选项的输出 1

=> <ActionController::Parameters {"option_id"=>"110", "option_quantity"=>"1"} permitted: false>

保留选项的输出 2

=> #<ActiveRecord::AssociationRelation [#<ReservationOption id: 62, option_id: 110, reservation_id: 142, option_quantity: nil, created_at: "2019-10-25 12:27:45", updated_at: "2019-10-25 12:27:45">]>


饮歌长啸
浏览 170回答 1
1回答

慕容3067478

我认为这可能会满足您的要求:class ReservationsController < ApplicationController&nbsp; def create&nbsp; &nbsp; @user = current_user&nbsp; &nbsp; @hotel = Hotel.find(params[:hotel_id])&nbsp; &nbsp; @reservation = @hotel.reservations.new(reservation_params)&nbsp; &nbsp; if @reservation.save&nbsp; &nbsp; &nbsp; params[:reservation_options_attributes].each do |reservation_option|&nbsp; &nbsp; &nbsp; &nbsp; if @option = Option.find_by(id: reservation_option[:option_id])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @reservation.options << @option&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reservation_option = @reservation.reservation_options.where(option: @option)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reservation_option.update(option_quantity: reservation_option[:option_quantity])&nbsp; &nbsp; &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; authorize @reservation&nbsp; &nbsp; &nbsp; redirect_to hotel_path(@hotel)&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; @room_type_list = @hotel.room_types&nbsp; &nbsp; &nbsp; render 'new'&nbsp; &nbsp; end&nbsp; endend自然而然地,这假定Reservation belongs_to :hotel您当前未在Reservation模型中显示的。还有,那个Hotel has_many :reservations。这也假设您的ReservationOption模型具有option_quantity属性。这似乎是拥有这种东西的自然场所。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript