将MAXSLOT方法添加到训练中

时间:2017-03-10 21:23:42

标签: ruby-on-rails ruby model-view-controller

我正在制作一个用户可以预订一小时培训的应用程序。我想给应用程序限制,当一个培训有24个用户预订时,没有人不能再预订(至少有一些用户删除了他的书),我的问题是我如何在培训模型中实现这个功能MAXSLOT以及我如何将其用于工作

class BookingsController < ApplicationController
  before_action :load_training,  only: [:create]

  def new
    @booking = Booking.new
    @training = Training.find(params[:training_id])
    @booking.training_id
  end

  def create
    @booking = @training.bookings.build(booking_params)
    @booking.user = current_user

    if @booking.save
      flash[:success] = "Book created"
      redirect_to trainings_path
    else
      render 'new'
    end
  end


  def index
    @bookings = Booking.all
  end


  def destroy
    @booking = Booking.find(params[:id])
    @booking.destroy
    flash[:success] = "Book deleted"
    redirect_to trainings_path
  end



private
  def booking_params
    params.require(:booking).permit(:user_id, :training_id)
  end

  def load_training
    @training = Training.find(params[:training_id])
  end

end

预订模式:

class Booking < ApplicationRecord
  belongs_to :user
  belongs_to :training
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :training_id, presence: true



end

我的routes.rb:

Rails.application.routes.draw do

  root 'static_pages#home'
  get    '/signup',               to: 'users#new'
  get    '/contact',              to: 'static_pages#contact'
  get    '/about',                to: 'static_pages#about'
  get    '/login',                to: 'sessions#new'
  post   '/login',                to: 'sessions#create'
  delete '/logout',               to: 'sessions#destroy'


  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]

  resources :trainings do
    resources :bookings
  end
  resources :users
end

培训模式:

class Training < ApplicationRecord
  has_many :users, through: :bookings
  has_many :bookings

end

训练控制员:

class TrainingsController < ApplicationController

  def show
    @training = Training.find(params[:id])
  end

  def index
    @trainings = Training.all
  end
end

培训观点索引:

<h1>Hours</h1>

<ul class="trainings">
  <% @trainings.each do |training| %>
  <li>
    <%= link_to training.hour, training_path(training) %>
  </li>
  <% end %>
</ul>

展示培训观点:

<div class="row">
    <section>
      <h1>
HOUR: <%= @training.hour %>
      </h1>
    </section>
    <section>
      <h1>
SLOTS: <%= @training.slots %>
      </h1>
    </section>
    <center>
    <%= render 'bookings/booking_form' if logged_in? %>
    <%= render 'bookings/index_bookings' if logged_in? %>
  </center>

这是我的squema.rb:

  create_table "bookings", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "training_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.index ["training_id"], name: "index_bookings_on_training_id"
    t.index ["user_id"], name: "index_bookings_on_user_id"
  end

  create_table "trainings", force: :cascade do |t|
    t.integer  "slots"
    t.text     "hour"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["hour"], name: "index_trainings_on_hour"
  end

由于

1 个答案:

答案 0 :(得分:1)

有几种方法可以解决这个问题。我建议这样的事情:

# Training
class Training < ApplicationRecord
  has_many :users, through: :bookings
  has_many :bookings

  # Check if anymore bookings can be added
  def can_book?
    bookings.count < slots # Depending on the exact logic of your app, it might make more sense to use users.count here instead. Your call.
  end
end

# Booking
class Booking < ApplicationRecord
  belongs_to :user
  belongs_to :training
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :training_id, presence: true

  # It might make sense to only validate this on create. Get rid of the `#` on the below line if you think so.
  validate :training_not_full?#, on: :create

  private

  def training_not_full?
    errors.add(:training, "The training session is full!") unless training.can_book?
  end
end

当您到达控制器中的if @booking.save时,会自动调用@booking.valid?。如果返回false,则@booking.save将不保存记录,并且也将返回false。这样,您可以通过模型中的自己的验证来控制持久性逻辑。控制器逻辑根本不需要改变。

我建议阅读有关rails验证here的内容。跳到相关部分的6.2。

另外,作为警告,default_scope通常最终会造成更多伤害。最终,您将拥有一个用例,您希望通过其他内容订购,并且您会发现自己经常在该范围内工作。如果你现在摆脱它,你可能会为自己省去一些麻烦。