【问题标题】:How to create JOIN tables in create action and assign relevant ids如何在创建操作中创建 JOIN 表并分配相关 ID
【发布时间】:2019-10-25 07:01:20
【问题描述】:

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

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

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

参数

 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

预订控制器

class ReservationsController < ApplicationController
  # skip_before_action :authenticate_user!
  def new
    @user = current_user
    @hotel = Hotel.find(params[:hotel_id])
    @reservation = Reservation.new
    @age_tables = @hotel.age_tables

    @reservation.build_reservation_contact
    @room_type_list = @hotel.room_types
    @all_options = @hotel.options
    @rooms = []
    @options = []
    @extra_guests = []

    if params[:room_type].present?
      @rooms = Room.find(params[:room_type]).rooms
      @options = Room.find(params[:room_type]).options
      @extra_guests = Room.find(params[:room_type]).extra_guests
    end
    if request.xhr?
      respond_to do |format|
        format.json {
        render json: {rooms: @rooms, options: @options, extra_guests: @extra_guests}
      }
        format.js
      end
    end

    authorize @reservation
  end

  def create
    def create
    @user = current_user
    @hotel = Hotel.find(params[:hotel_id])
    @reservation = Reservation.new(reservation_params)
    @reservation.hotel = @hotel
    authorize @reservation
    if @reservation.save
      params[:reservation_options_attributes].each do |reservation_option|
        if @option = Option.find_by(id: reservation_option[:option_id])
          @reservation.options << @option
          # output 1 for reservation_option

          reservation_option = @reservation.reservation_options.where(option: @option)
          # output 2 for reservation_option

          reservation_option.update(option_quantity: reservation_option[:option_quantity])
          # Error message

        end
      end

      redirect_to root_path
    else
      @room_type_list = @hotel.room_types
      render 'new'
    end
  end
private
  def reservation_params
      params.require(:reservation).permit(:room_id, :arrival, :departure, :payment, :reservation_contact_id, option_ids:[],
      reservation_contact_attributes: [:id, :first_name,
      :last_name, :first_name, :last_name, :zipcode, :city, :street, :street_number,
      :email, :phone, :date_of_birth, :country, :company, :gender, :vat_number],
        rooms_attributes: [:id,:name, :room_type_id,
          room_types_attributes: [:id, :name]],
      reservation_options_attributes: [:id, :option_id, :option_quantity, :_destroy,
        options_attributes: [:id, :name, :room_type_id, :description,
          room_types_attributes:[:id, :name]]],
      reservation_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

reservation_option 的输出 1

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

reservation_option 的输出 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">]>

【问题讨论】:

    标签: javascript jquery ruby-on-rails activerecord


    【解决方案1】:

    我认为这可能会满足您的需求:

    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 属性。这似乎是一个自然的地方拥有这样的东西。

    【讨论】:

    • 谢谢(再次)。我现在可以生成 reservation_options 和 options,直到分配 option_quantity(例如reservation_option.update(quantity: reservation_option[:option_quantity]))。在这里我收到错误no implicit conversion of Symbol into Integer
    • 仅供参考。我用最新动态更新了我的问题。
    • 谢谢,您的回答给了我 90% 的解决方案。最后,我通过识别@option_quantity = reservation_option[:option_quantity] 并以这种方式推送它而不是reservation_option.update(option_quantity: @option_quantity) 来解决它
    猜你喜欢
    • 2011-05-05
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多