Consulta

mail@pastecode.io avatar
unknown
ruby
2 years ago
3.7 kB
1
Indexable
Never
#Estas son mis asociaciones:

class ForeignBooking < ApplicationRecord
  belongs_to :motel
  has_one :occupancy, foreign_key: 'occupancy_foreign_booking_id', dependent: :destroy
  has_one :room, through: :occupancy, foreign_key: 'occupancy_foreign_booking_id'
end

class Occupancy < ApplicationRecord
  belongs_to :room, foreign_key: 'occupancy_room_id'
  belongs_to :foreign_booking, foreign_key: 'occupancy_foreign_booking_id', optional: true
end

class Room < ApplicationRecord
  has_many :occupancies, foreign_key: 'occupancy_room_id'
  has_many :bookings, through: :occupancies, foreign_key: 'occupancy_room_id'
  has_many :foreign_bookings, through: :occupancies, foreign_key: 'occupancy_room_id'
end

# Este es el f.select del new/form, Acá asigno los rooms de ese motel

        <div class="form-group col-md-6">
          <%= f.label :room_id, '# Habitación' %>
          <%= select_tag :room_id, options_for_select(@motel.rooms.map { |r| [r.room_name, r.room_id] }), :class => "form-control select", include_blank: true %>
        </div>


# Este es mi create action ya que deseo crear una ocupación en el ForeignBookingController

  def create
    @motel = Motel.find(params[:motel_id])
    authorize @motel, :show?
    
    @booking = @motel.foreign_bookings.new(create_params)
    authorize @booking
    
    @booking.backend_user_id = current_user.id
    
    @booking_type = create_params[:booking_type]

    if @booking.save
    # Se asocia el foreign booking con el occupancy
      if params[:room_id].present?
        new_occupancy = {
          occupancy_foreign_booking_id: @booking.id,
          occupancy_room_id: params[:room_id],
          occupancy_start: @booking.booking_time,
          occupancy_end: @booking.booking_time_end
        }
        Occupancy.create!(new_occupancy)
      end
      flash[:success] = 'La reserva se registró correctamente en el libro'
      redirect_to motel_foreign_bookings_path(@motel, booking_type: @booking_type)
    else
      flash.now[:error] = 'Ocurrió un error. Por favor revisa los datos de ingreso.'
      render 'new'
    end
  end

# Este es mi edit form de ese mismo f.select para poder tener otros rooms en caso el usuario quiera cambiar o asignar otra habitación

        <div class="form-group col-md-6">
          <%= f.label :room_id, '# Habitación' %>
          <%= select_tag :occupancy_room_id, options_for_select([@motel.rooms.map(&:room_name)], selected: @booking.occupancy), :class => "form-control select", include_blank: false, disabled: true %>
        </div>

# Este es mi update action

  def update
    @motel = Motel.find(params[:motel_id])
    authorize @motel, :show?

    @booking = @motel.foreign_bookings.find(params[:id])
    authorize @booking

    @booking.backend_user_id = current_user.id

    @booking_type = update_params[:booking_type]
    
    if @booking.update(update_params)
      # Se actualiza el occupancy de ese foreign booking
      byebug
      if params[:room_id].present?
        occupancy_update = {
          occupancy_foreign_booking_id: @booking.id,
          occupancy_room_id: params[:room_id],
          occupancy_start: @booking.booking_time,
          occupancy_end: @booking.booking_time_end
        }
        @booking.occupancy.update!(occupancy_update)
      end
      flash[:success] = 'La reserva se modificó correctamente en el libro'
      redirect_to motel_foreign_bookings_path(@motel, booking_type: @booking_type)
    else
      flash.now[:error] = 'Ocurrió un error. Por favor revisa los datos de ingreso.'
      render 'edit'
    end

  end