亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在創建操作中創建 JOIN 表并分配相關 id

如何在創建操作中創建 JOIN 表并分配相關 id

飲歌長嘯 2021-12-02 19:53:02
對于房間預訂,我想包括房間選項。選項及其連接表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"=>"[email protected]", "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_optionsendclass ReservationOption < ApplicationRecord  belongs_to :option  belongs_to :reservation  accepts_nested_attributes_for :optionendclass Option < ApplicationRecord  belongs_to :room_type  has_many :reservation_options, dependent: :destroy  has_many :reservations, through: :reservation_options  validates :name, presence: trueend保留選項的輸出 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">]>
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

我認為這可能會滿足您的要求:


class ReservationsController < ApplicationController


  def create

    @user = current_user

    @hotel = Hotel.find(params[:hotel_id])

    @reservation = @hotel.reservations.new(reservation_params)

    if @reservation.save

      params[:reservation_options_attributes].each do |reservation_option|

        if @option = Option.find_by(id: reservation_option[:option_id])

          @reservation.options << @option

          reservation_option = @reservation.reservation_options.where(option: @option)

          reservation_option.update(option_quantity: reservation_option[:option_quantity])

        end

      end

      authorize @reservation

      redirect_to hotel_path(@hotel)

    else

      @room_type_list = @hotel.room_types

      render 'new'

    end

  end


end

自然而然地,這假定Reservation belongs_to :hotel您當前未在Reservation模型中顯示的。還有,那個Hotel has_many :reservations。


這也假設您的ReservationOption模型具有option_quantity屬性。這似乎是擁有這種東西的自然場所。


查看完整回答
反對 回復 2021-12-02
  • 1 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號