mysql - rails saves wrong value to database -
i'm not sure changed code try put value 7777777777777 database puts old serial_number 2147483647 value stored in database (and when removed record 2147483647 serial nunmber) still inserts record 2147483647 instead of 7777777777777 in database:
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 purchase 1 airtime plan @ time"} render "airtime_plans/show" , 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" , return end args[:resource] = @airtime_plan end args[:serial_number] = params[:serial_number] @line_item = @cart.add_sellable_item args #save line item update quantity, save association 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
cart model
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 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
i tried using reload, tried forcing create! , tried explicitly setting serial_number, , still creates record serial_number of 2147483647. have validates_uniquness_of constraints on line item model.
check log out, makes sense:
started post "/line_items" 127.0.0.1 @ 2013-05-21 14:01:18 -0400 processing lineitemscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"ioxew7itmoysfrxho3cywyb2b7tlfhvr7jgwhwxqwxg=", "serial_number"=>"102099394", "unit_type"=>"marinetrac", "sellable_id"=>"118", "sellable_type"=>"airtimeplan", "commit"=>"add cart"} (0.1ms) begin sql (0.4ms) insert `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' key 'index_line_items_on_serial_number': insert `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' key 'index_line_items_on_serial_number': insert `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'
Comments
Post a Comment