0

我建立Rails中的活動應用程序,我已經打了上面這涉及到這個方法在我的模型錯誤 -Rails的名稱錯誤 - 未定義的局部變量或方法'預訂」

def validate_availability 
     errors.add(:base, 'event is fully booked') if booking.count >= event.number_of_spaces 
    end 

目的該方法的目的是避免過度預訂具有特定數量的空間的事件。在我的控制器,我有以下的代碼 -

控制器#創建

def create 

    @event = Event.find(params[:event_id]) 
    @booking = @event.bookings.new(booking_params) 
    @booking.user = current_user 

     if 
      @booking.set_booking 
      flash[:success] = "Your place on our event has been booked" 
      redirect_to event_booking_path(@event, @booking) 
     else 
      flash[:error] = "Booking unsuccessful" 
      render "new" 
     end 

     if @event.is_free? 
      @booking.save(booking_params) 
     end 

     if booking.count >= @event.number_of_spaces 
      flash[:error] = "Sorry, this event is now fully booked" 
      render "new" 
     end 
end 

我需要在我的控制器來定義booking.count但不知道什麼工作 - 嘗試了一些東西,但工作空話。我在我的架構下 -

create_table "bookings", force: :cascade do |t| 
    t.integer "event_id" 
    t.integer "user_id" 
    t.string "stripe_token" 
    t.datetime "created_at",     null: false 
    t.datetime "updated_at",     null: false 
    t.integer "quantity",   default: 1 
    t.integer "total_amount" 
    t.string "stripe_charge_id" 
    t.string "booking_number" 
    end 

的booking.count將依賴空間/預訂的用戶希望對剩餘空間的數量,但是我怎麼表達這個量是多少?我需要在我的表格中使用total_bookings欄還是使用單獨的方法?

更新 -

Booking.rb

class Booking < ActiveRecord::Base 

    belongs_to :event 
    belongs_to :user 
    before_create :set_booking_number 


    validates :quantity, presence: true, numericality: { greater_than_or_equal_to: 0 } 
    validates :total_amount, presence: true, numericality: { greater_than_or_equal_to: 0 } 

    validate(:validate_booking) 
    validate(:validate_availability) 

    def set_booking_number 
    self.booking_number = "MAMA" + '- ' + SecureRandom.hex(4).upcase 
    end 

    def set_booking 

     if self.event.is_free? 
      self.total_amount = 0 
      save! 
     else 
      self.total_amount = event.price_pennies * self.quantity 
      begin 
      charge = Stripe::Charge.create(
       amount: total_amount, 
       currency: "gbp", 
       source: stripe_token, 
       description: "Booking created for amount #{total_amount}") 
      self.stripe_charge_id = charge.id 
      save! 
      rescue Stripe::CardError => e 
      # if this fails stripe_charge_id will be null, but in case of update we just set it to nil again 
      self.stripe_charge_id = nil 
      # we check in validatition if nil 

      end 

     end 


    end 

     def validate_booking 

     # stripe_charge_id must be set for not free events 
     unless self.event.is_free? 
      return !self.stripe_charge_id.nil? 
     end 
     end 

     private 

     def validate_availability 
      errors.add(:base, 'event is fully booked') if event.bookings.count >= event.number_of_spaces 
     end 

end 
+1

豈不是與事件有關? event.booking.count? –

+0

你的關係叫'預訂' 所以它應該是'bookings.count' –

+0

公平點,但我仍然會得到同樣的錯誤不是嗎? –

回答

0

,預約表的計數,你應該在事件表內booking_count場。爲此使用計數器緩存。欲瞭解更多詳情,請查詢http://guides.rubyonrails.org/association_basics.html。當記錄很大時這非常有用。

你的遷移將列應如下類似:

def change 
    add_column :events, :bookings_count, :integer, default: 0 
    Event.reset_column_information 
    Event.all.each do |e| 
     Event.update_counters e.id, :bookings_count => e.bookings.length 
    end 
    end 
+0

因此,我會將counter_cache添加到我的Bookings模型中,並將events_precount添加到事件中,並且與上述方法相關地將其更改爲event.booking_count> = event.number_of_spaces?也可以將此方法從預訂轉移到事件模型。 –

+0

不,將它保留在預訂模型中,並使用比較event.booking_count> = event.number_of_spaces –

+0

務必更新之前添加的活動的預訂計數 –

相關問題