[英]Rails - NoMethodError undefined method `>=' for nil:NilClass
I'm getting the above error in my model. 我在模型中遇到上述错误。 Here's the model code - 这是型号代码 -
Booking.rb 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 ? 我是否进行了change_column_null迁移或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). 很明显,没有为数量设置(也不是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. 我试图通过下面的before_validation回调和方法来解决这个问题,但它们似乎没有起作用。 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 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 结束
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
. 如果set_default_values_to_greater_than_or_equal_to_zero
尝试设置这些属性的默认值,则更改>=
to =
因为>=
是一个将返回true
或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: 看到:
As is noted in the change_column_default page comments, change column is the preferred way to do this 如change_column_default页面注释中所述,更改列是执行此操作的首选方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.