Rails + Money gem:验证一个Money字段大于另一个

时间:2014-06-19 19:58:10

标签: ruby-on-rails

我在确认使用money rails gem(https://github.com/RubyMoney/money-rails)确保一列大于另一列时遇到问题。

我有一个FlatCoupon模型。它有两个字段:minimum_purchase_amount(钱)和value(十进制)。

架构:

create_table "coupons", force: true do |t|
  ...
  t.integer  "minimum_purchase_amount_cents",                             default: 0,     null: false
  t.string   "minimum_purchase_amount_currency",                          default: "USD", null: false
  t.decimal  "value",                            precision: 30, scale: 2

当我将此验证添加到我的FlatCoupon模型时:

class FlatCoupon < Coupon
  monetize :minimum_purchase_amount_cents
  validates :minimum_purchase_amount, numericality: { greater_than: ->(flat_coupon) { Money.new(flat_coupon.value*100) }, message: "must be more than the discount amount." }, 
            if: Proc.new { |c| c.value.present? } 

导致我的FactoryGirl工厂失败:

FactoryGirl.define do
  factory :flat_coupon do
    ...
    minimum_purchase_amount 30.33
    value 12.00
  end
end

是不是将value属性设置为小于minimum_purchase_amount?看看我的控制台:

    irb(main):001:0> f = FactoryGirl.build(:flat_coupon)
    => #<FlatCoupon id: nil, start_date: "2014-06-18 00:00:00", end_date: "2014-06-20 00:00:00", minimum_purchase_amount_cents: 3033, minimum_purchase_amount_currency: "USD", value: #<BigDecimal:7fe37f8043e0,'0.12E2',9(45)>, type: "FlatCoupon", created_at: nil, updated_at: nil, name: "Flat!", site_wide: nil, code: "BBB321">
    irb(main):002:0> f.valid?
      Coupon Exists (0.7ms)  SELECT  1 AS one FROM `coupons`  WHERE `coupons`.`code` = BINARY 'BBB321' LIMIT 1
    => false
    irb(main):003:0> f.errors
    => #<ActiveModel::Errors:0x007fe37f8b1540 @base=#<FlatCoupon id: nil, start_date: "2014-06-18 00:00:00", end_date: "2014-06-20 00:00:00", minimum_purchase_amount_cents: 3033, minimum_purchase_amount_currency: "USD", value: #<BigDecimal:7fe37f8043e0,'0.12E2',9(45)>, type: "FlatCoupon", created_at: nil, updated_at: nil, name: "Flat!", site_wide: nil, code: "BBB321">, @messages={:minimum_purchase_amount=>["must be more than the discount amount."]}>
    irb(main):004:0> f.minimum_purchase_amount > Money.new(f.value*100)
    => true

任何人都可以帮助我吗?

即使我更改验证以逐个打印出值:

validates_numericality_of :minimum_purchase_amount, greater_than: ->(flat_coupon) { 
  puts Money.new(flat_coupon.value*100);
  puts flat_coupon.minimum_purchase_amount;
  puts flat_coupon.minimum_purchase_amount > Money.new(flat_coupon.value*100);
  Money.new(flat_coupon.value*100) }

它仍然这样做:

    Nets-Mac-Pro:ilook emai$ be rails c
    Loading development environment (Rails 4.1.1)
    irb(main):001:0> flat_coupon = FactoryGirl.build(:flat_coupon)
    => #<FlatCoupon id: nil, start_date: "2014-06-18 00:00:00", end_date: "2014-06-20 00:00:00", minimum_purchase_amount_cents: 3033, minimum_purchase_amount_currency: "USD", value: #<BigDecimal:7fe379732ad0,'0.12E2',9(45)>, type: "FlatCoupon", created_at: nil, updated_at: nil, name: "Flat!", site_wide: nil, code: "BBB321">
    irb(main):002:0> flat_coupon.valid?
      Coupon Exists (0.6ms)  SELECT  1 AS one FROM `coupons`  WHERE `coupons`.`code` = BINARY 'BBB321' LIMIT 1
    12.00
    30.33
    true
    => false

这笔交易是什么?这是一个错误吗?

P.S。由于商业原因,我无法将数值更改为数据库中的货币值。

0 个答案:

没有答案