1
votes

I'm getting the above error in my model. Here's the model code -

Booking.rb

    class Booking < ActiveRecord::Base

    belongs_to :event
    belongs_to :user

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

    before_validation :set_default_values_to_greater_than_or_equal_to_zero

    def set_default_values_to_greater_than_or_equal_to_zero
        self.quantity >= 1
        self.total_amount >= 0  
        self.event.price >= 1    unless self.event.is_free
    end

    def reserve(stripe_token)
        # Don't process this booking if it isn't valid
        self.valid?

        # We can always set this, even for free events because their price will be 0.
        #self.total_amount = booking.quantity * event.price

                # Free events don't need to do anything special
                if event.is_free?
                save!

                # Paid events should charge the customer's card
                else

                    begin
                        self.total_amount = event.price * self.quantity
                        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
                    errors.add(:base, e.message)
                    false
                end
            end 
        #end
    end
end

The error code is thrown on this line -

self.quantity >= 1

I believe this is because of the default value (or lack of one) on this particular column/attribute. What is the correct process to correct this in migrations? Do I conduct a change_column_null migration or change_column_default ?

This is my table -

 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"
    t.integer  "total_amount"
    t.string   "stripe_charge_id"
  end

As is evident, there is nothing set for quantity (nor total_amount). I've tried to correct this with the before_validation callback and method below this but they don't appear to be working. I believe the migration is the only way to solve this, am I correct?

This is my controller code, is there anything I need to put in here?

bookings_controller.rb

 class BookingsController < ApplicationController

before_action :authenticate_user!

def new
    # booking form
    # I need to find the event that we're making a booking on
    @event = Event.find(params[:event_id])
    # and because the event "has_many :bookings"
    @booking = @event.bookings.new(quantity: params[:quantity])
    # which person is booking the event?
    @booking.user = current_user


end

def create

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

        if 
            @booking.reserve(booking_params['stripe_token'])
            flash[:success] = "Your place on our event has been booked"
            redirect_to event_path(@event)
        else
            flash[:error] = "Booking unsuccessful"
            render "new"
        end
end


private

def booking_params
    params.require(:booking).permit(:stripe_token, :quantity, :event_id, :stripe_charge_id, :total_amount)
end

end

1
try this, to rescue exception self.try(:quantity) >= 1Navin
you say "set" quantity, but ">=" is a comparison operator, not an assignment operator. What are you trying to achieve in set_default_values_to_greater_than_or_equal_to_zero this method? Also, I believe that method should be a private method, not a public one.Eric
I'm trying to give quantity and total_amount a default value so I don't get the above error. In addition, if you note further down I want a user to be able to add a number of spaces when they book onto an event. So one space costs £10 but they want to book, say, 5 spaces, therefore they would need to pay £50 so total_amount = event.price * quantity (number of spaces required)Mike.Whitehead
You can define a default right in your migration if you wish. t.integer "quantity", default: 8jaydel

1 Answers

0
votes

If set_default_values_to_greater_than_or_equal_to_zero was an attempt to set a default for those attributes change >= to = because >= is an evaluation operator which will return true or false. If you go this route, I would suggest to make that a private method.

If you want to make a migration instead, to set defaults on the database level, (which I would favor over a before validation callback),

change_column_default(:bookings, :quantity, 1)

or

change_column :bookings, :quantity, :integer, :default => 1

See:

change column default

change column

As is noted in the change_column_default page comments, change column is the preferred way to do this