简体   繁体   English

通过关系创建 has_one

[英]Creating a has_one through relationship

In my reservation form I would like to create a situation where a user can book one room.在我的预订表格中,我想创建一个用户可以预订一个房间的情况。 As I need to have a unique identifier for a room that is reserved, I use a setup where room and reservation are linked via reservation_rooms .正如我需要为保留一个房间一个唯一的标识符,我用其中的房间,并保留通过链接的设置reservation_rooms

  • A reservation_room belongs to a reservation and a room一个reservation_room属于一个reservation和一个room
  • A reservation has_one room reservation _一个room

Question

I'm trying to let a user pick a room and consequently build the reservation_room model with this reservation and the room picked我试图让用户选择一个房间,并因此建立reservation_room有这位模特reservationroom picked

My current setup doesn't allow me to get the room in the first place (in the create action):我当前的设置不允许我首先获得房间(在创建操作中):

Processing by ReservationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YfVsfb4po0Dl/kycf9Mm6LMYCXi7+GLDUpe8gANaGRMNzPiCoq6HC9CWarSyJjLmYG1/hE6D7w==", "reservation"=>{"arrival"=>"2019-12-05", "departure"=>"2019-12-06", "room_id"=>"7"}, "commit"=>"Search", "hotel_id"=>"22"}

Room.find(params[:room_id])
ActiveRecord::RecordNotFound: Couldn't find Room without an ID

Code代码

models楷模

models
class Reservation < ApplicationRecord
  belongs_to :hotel
  has_one :reservation_room, dependent: :destroy
  has_one :room, through: :reservation_rooms
end

class ReservationRoom < ApplicationRecord
  belongs_to :room
  belongs_to :reservation
end

class Room < ApplicationRecord
  belongs_to :room_category
  has_many :reservation_rooms
  has_many :reservations, through: :reservation_rooms, dependent: :destroy
  accepts_nested_attributes_for :room_category
end

reservations_controller预订控制器

def new
    @hotel = Hotel.find(params[:hotel_id])
    @reservation = @hotel.reservations.new
    @room_categories = @hotel.room_categories
    @rooms = Room.where(room_category: @room_categories)
    authorize @reservation
  end

  def create
    @hotel = Hotel.find(params[:hotel_id])
    @reservation = @hotel.reservations.new(reservation_params)
    binding.pry
    @room = Room.find(params[:room_id])
    @reservation.build_reservation_room(room_id: @room.id)
    authorize @reservation
    if @reservation.save
      authorize @reservation
      redirect_to new_second_part_hotel_reservation_path(@hotel, @reservation)
    end
  end

  private

  def reservation_params
      params.require(:reservation).permit(:room_id, :arrival, :departure,
      reservation_room_attributes: [:room_id, :reservation_id],
        rooms_attributes: [:id,:name, :room_category_id,
          room_categories_attributes: [:id, :name]])
  end
end

reservations/new.html.erb预订/new.html.erb

  <%= simple_form_for [@hotel, @reservation] do |f|%>
        <%= f.input :arrival%>
        <%= f.input :departure %>
        <%= f.input :room_id, collection: @room_categories.order(:name), as: :grouped_select, group_method: :rooms,  label:false %>
        <%= f.button :submit %>
    <% end %>

Having a join table makes sense if you you have a many to many assocation (a reservation can reserve multiple rooms) but if you just want a one to many association you just want to place a foreign key on reservations and use belongs_to:如果您有多个关联(预订可以预订多个房间),那么拥有连接表是有意义的,但如果您只想要一对多关联,您只想在预订上放置一个外键并使用belongs_to:

rails g migration add_room_to_reservations room:belongs_to

class Reservation
  belongs_to :room
end

class Room
  has_many :reservations
end

IMHO one reservation per room is probably a lot more sane.恕我直言,每个房间预订一次可能要理智得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM