我不确定,但是我更改了这段代码,试图让它将值7777777777777放入数据库中,它总是把这个旧的serial_number 2147483647值存储在数据库中(甚至当我用2147483647串行nunmber删除该记录时)它仍然在数据库中插入记录2147483647而不是7777777777777:
def create
@cart = current_cart
quantity = nil
resource = nil
args = {}
args[:quantity] = is_number?(params[:quantity]) ? params[:quantity].to_i : 1
if params[:sellable_type] == "Product"
@product = Product.find(params[:sellable_id])
args[:resource] = @product
elsif params[:sellable_type] == "AirtimePlan"
@airtime_plan = AirtimePlan.find(params[:sellable_id])
if @cart.airtime_list_items.any?
flash[:notice] = {:too_many_plans => "Can only purchase one airtime plan at a time"}
render "airtime_plans/show" and return
end
if ['MarineTrac','MotoTrac','FleetTrac','AutoTrac'].include? params[:unit_type]
args[:unit_type] = params[:unit_type]
else
flash[:notice] = {:nonexistant_plan => "No such plan exists"}
render "airtime_plans/show" and return
end
args[:resource] = @airtime_plan
end
args[:serial_number] = params[:serial_number]
@line_item = @cart.add_sellable_item args
#save line item to update its quantity, this will also save association with carton
if @line_item.save
redirect_to cart_path(@cart), notice: 'Line item created.'
else
if params[:sellable_type] == "Product"
render "products/show"
else
flash[:notice] = @line_item.errors.messages
render "airtime_plans/show"
end
end
end
def add_sellable_item(args={})#resource, _quantity, serial_number=nil
resource = args[:resource]
_quantity = args[:quantity]
serial_number = args[:serial_number] if args[:serial_number]
unit_type = args[:unit_type] if args[:unit_type]
current_item = self.line_items.where(sellable_type: resource.class.name, sellable_id: resource.id).first
if current_item && current_item.sellable.is_a?(Product)
current_item.quantity += _quantity
else
new_item = self.line_items.reload.create!(sellable_id: resource.id, sellable_type: resource.class.name, serial_number: 7777777777777)
puts "What is the serial number #{new_item.serial_number}"
new_item.quantity = _quantity
new_item.unit_type = unit_type if unit_type
return new_item
end
current_item
end
我尝试使用重装,我尝试强制创建!我尝试显式设置serial_number,它仍然创建一个serial_number为2147483647的记录。我甚至在行项目模型上有validates_uniquness_of约束。
检查此注销,这是有道理的:
Started POST "/line_items" for 127.0.0.1 at 2013-05-21 14:01:18 -0400
Processing by LineItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IOXew7ITmoysfrxHO3CYwYB2b7TLFhVR7JgwhwxqwXg=", "serial_number"=>"102099394", "unit_type"=>"MarineTrac", "sellable_id"=>"118", "sellable_type"=>"AirtimePlan", "commit"=>"Add to Cart"}
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')
Mysql2::Error: Duplicate entry '2147483647' for key 'index_line_items_on_serial_number': INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')
(0.5ms) ROLLBACK
Completed 500 Internal Server Error in 14ms
ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '2147483647' for key 'index_line_items_on_serial_number': INSERT INTO `line_items` (`cart_id`, `created_at`, `order_id`, `quantity`, `sellable_id`, `sellable_type`, `serial_number`, `unit_type`, `updated_at`) VALUES (NULL, '2013-05-21 18:01:18', NULL, 1, NULL, NULL, 7777777777777, NULL, '2013-05-21 18:01:18')):
app/controllers/line_items_controller.rb:8:in `create'