方法在开发中有效,但在生产中无效 Rails MongoDB

Method works in development but not production Rails MongoDB

我有一张优惠券 class,我希望我的应用程序检查并查看优惠券上剩余的计数以及优惠券的日期是否已过期。我的 class 中有以下方法来检查这两个。

Coupon class

  def self.get(code)
   where(
    :code => (normalize_code(code)),
    :$and => [
      {
       :$or => [
         { :coupon_count.gte => 1  },
         { :coupon_count    => nil }
       ]
     }, {
       :$or => [
         { :expires_at.gt => Time.now.utc },
         { :expires_at    => nil      }
      ]
     }
    ]
  ).first
 end

当我输入优惠券时,这在开发中运行良好。但在生产中它不起作用。我使用我的 MongoDB shell 创建优惠券如下。

db.Coupon.insert({code:'#COUPONNAME',discount_percent: 10, expires_at: new ISODate("2016-05-18"), coupon_count: 10, "description": '1st cold visit sign-up'})

似乎问题出在优惠券检查 expires_at 日期时。在开发中,它找到了优惠券并开始工作,但在生产中它一直找不到优惠券。为了更好的衡量,这里是我的控制器方法。

编辑 我认为问题出在日期上,但如果我删除日期查询,它在生产中仍然不起作用。我很困惑为什么这在生产中不起作用。它正在使用 MongoDB 3.0.10 和 mongoid 5.1.0 gem


charges_controller
  @code = params[:couponCode]

if !@code.blank?
  @coupon = Coupon.get(@code)

  if @coupon.nil?
    flash[:error] = 'Coupon code is not valid or expired.'
    redirect_to new_managers_charge_path(id: @reportapproval.id)
    return
  elsif @coupon.discount_percent == 100
    @reportapproval.report_paid = true
    @reportapproval.free_coupon_used = true
    @reportapproval.save!
    @coupon.coupon_count = @coupon.coupon_count - 1
    @coupon.save!
    redirect_to managers_dashboard_path, :notice => "You have successfully requested a pre-paid report from #{@reportapproval.tenant_last_name} with a 'No-Pay' intro coupon."
    return
  else
    @final_amount = @coupon.apply_discount(@amount.to_i)
    @discount_amount = (@amount.to_i - @final_amount.to_i)
  end

如果您有一个 Coupon Mongoid 模型,那么 MongoDB shell 中的集合将是 db.coupons。这可以解释为什么:

db.Coupon.insert(...)

在 MongoDB shell 中没有提供您期望在 Rails 代码中找到的内容。


就 Neil 关于 $exists 与显式 nil 检查的评论而言,我认为您确实想要 nil(又名 null inside MongoDB ) 检查。在 MongoDB shell:

中考虑这一点
> db.models.insert({ n: 11 })
> db.models.insert({ n: 0 })
> db.models.insert({ n: null })
> db.models.insert({ })
> db.models.find()
{ "_id" : ObjectId("571546e1ce2934dadf379479"), "n" : 11 }
{ "_id" : ObjectId("571546e4ce2934dadf37947a"), "n" : 0 }
{ "_id" : ObjectId("571546e7ce2934dadf37947b"), "n" : null }
{ "_id" : ObjectId("571546ecce2934dadf37947c") }

所以我们有一个包含 n、没有 nn 有显式 null 值和没有 [=] 的文档的集合n.

的 20=] 值

然后我们可以看到像 :n => nil:

这样的 Mongoid 查询之间的区别
> db.models.find({ n: null })
{ "_id" : ObjectId("571546e7ce2934dadf37947b"), "n" : null }
{ "_id" : ObjectId("571546ecce2934dadf37947c") }

:n.exists => true(又称:n => { :$exists => true }):

> db.models.find({ n: { $exists: true } })
{ "_id" : ObjectId("571546e1ce2934dadf379479"), "n" : 11 }
{ "_id" : ObjectId("571546e4ce2934dadf37947a"), "n" : 0 }
{ "_id" : ObjectId("571546e7ce2934dadf37947b"), "n" : null }

:n => { :$exists => false }

> db.models.find({ n: { $exists: false } })
{ "_id" : ObjectId("571546ecce2934dadf37947c") }

因此 :expires_at => nil 查询将查找没有 expires_at 的文档以及 expires_at 明确设置为 nil 的文档。这两种情况都会发生在 Mongoid 中,除非你小心地调用 remove_attribute 而不是分配 nil 并且这两种情况都意味着 "no expiry date".