【问题标题】:virtual value total_price is saved as 1 instead of total price number虚值 total_price 保存为 1 而不是总价格数
【发布时间】:2012-04-18 21:58:13
【问题描述】:

total_pricecart 中的所有产品中未按应有的方式保存。无论实际总价是多少,它总是显示 1。这个问题是在安装money gemgoogle_currency gem 之后出现的,所以我可以根据选择的语言环境更改产品本身的价格,而不仅仅是货币。

所以总价始终为 1,但同时我在购物过程中屏幕上显示的金额显示正确的数字,我的意思是如果我选择价格为 A 和 B 的 2 个产品,然后在购物车中(在屏幕上)我有正确的 total_price = A+B .. 不仅如此,当我在填写信用卡字段后按下创建订单时,ActiveMerchant 将数据发送到贝宝和美元金额to paypal 是我在购物车 A+B 中的总价格 ..

问题是:为什么app取了正确的total_price并发送到paypal,但同时又不能在创建订单时把这个total_price存入数据库,它保存1 代替。

当我创建 orderconsole 说了什么:

   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `orders` (`card_expires_on`, `card_type`, `cart_id`, `created_at`, `first_name`, `ip_address`, `last_name`, `total`, `updated_at`, `user_id`) VALUES ('2012-04-01', 'visa', 5, '2012-04-18 21:35:38', 'Rosca', '127.0.0.1', 'Sergiu', 1, '2012-04-18 21:35:38', 7)

order_transaction详情:

SQL (0.5ms)  INSERT INTO `order_transactions` (`action`, `amount`, `authorization`, `created_at`, `message`, `order_id`, `params`, `success`, `updated_at`) VALUES ('purchase', 1, '0GT41652PP785722H', '2012-04-18 21:35:46', 'Success', 5, '--- \nbuild: \"2764190\"\nAck: Success\ntimestamp: \"2012-04-18T21:35:45Z\"\nTransactionID: 0GT41652PP785722H\namount: \"42.00\"\namount_currency_id: USD\ntransaction_id: 0GT41652PP785722H\nack: Success\nBuild: \"2764190\"\navs_code: X\nversion: \"62.0\"\nTimestamp: \"2012-04-18T21:35:45Z\"\nCorrelationID: 690aa904db3c\nAmount: \"42.00\"\nAVSCode: X\nVersion: \"62.0\"\ncvv2_code: M\nCVV2Code: M\ncorrelation_id: 690aa904db3c\n', 1, '2012-04-18 21:35:46')

关于如何创建订单的一些细节:

orders_controller.rb

  def create
    @order = current_cart.build_order(params[:order])
    @order.user_id = current_user.id
    @order.total = current_cart.total_price
#    raise current_cart.total_price.inspect
    @order.line_items = current_cart.line_items
    @order.ip_address = request.remote_ip
    if @order.save
      if @order.purchase
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
      else
        render :action => "failure"
      end
        respond_to do |format|
          format.html { redirect_to products_path, :notice => 
          'Thank you for your order.' }
          format.json { render :json => @order }
        end
    else
       render :action => 'new'
    end
  end

注意如果我在控制器中激活提升检查线,我会得到

#<Money cents:4200 currency:USD> 错误,在这种情况下,4200 是 2 个选定产品的 total_price。 42.00 美元。 这再次证明了 total_price 公式在应用需要将其存储在数据库中之前都可以正常工作。

总价:

total_pricecart.rbline_item.rb

中形成

line_item.rb

  belongs_to :order
  belongs_to :product
  belongs_to :cart
  belongs_to :user

  def total_price
    product.price * quantity
  end

cart.rb

has_many :line_items#, :dependent => :destroy
  has_one :order

  def to_s
    id
  end

  def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(:product_id => product_id)
    end
    current_item
  end

  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end

编辑

数据库中的 order.total 字段类型现在是整数 (11),但我尝试使用小数 (10,0) 以美分存储价格,但无论如何它都不起作用,我总是得到 1 美元的总价。 products 的价格为十进制 (10,0)。

真的很大感谢您的帮助..可能对未来的开发人员有用。

您可能需要的任何其他信息,请告诉我。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1


    【解决方案1】:

    好吧,我将order.total 的字段从decimal 更改为float 类型,现在它工作正常。我有正确的金额发送到贝宝,以及为用户创建的订单中的正确金额。在使用 money gem 之前,它可以很好地与 decimal 字段类型一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多