每间餐厅每天最多可预订15个座位。所有餐厅一天的预订不应超过20个。
我希望能够用ActiveRecord做一个“条件”,其中预订日期等于参数传递的日期
def create
@reservation = Reservation.new(reservation_params)
@restaurant_date = @reservation.startreservation.strftime("%F")
pp @restaurant_date
@restaurant_reservation = Reservation.where(restaurant_id: @reservation.restaurant_id).where(:startreservation.strftime("%F") => @restaurant_date )
pp @restaurant_reservation
# if @restaurant_reservation == 15
# redirect_to restaurants_path , alert: "Reservation was not created."
# else
if @reservation.save
redirect_to reservation_url(@reservation), notice: "Reservation was successfully created."
else
render :new, status: :unprocessable_entity
end
#end
end
可能是我没有很好地解释问题,任何问题我都会在此刻回答。:)
2条答案
按热度按时间h5qlskok1#
它是业务逻辑(检查15个注册),因此最佳实践是将其放置在模型或服务中(而不是控制器中),例如在模型中使用自定义验证:
...控制器:
ActiveRecord验证
niknxzdl2#