方法适用于开发但不适用于生产Rails MongoDB

时间:2016-04-17 14:10:04

标签: ruby-on-rails mongodb mongoid

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

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

1 个答案:

答案 0 :(得分:2)

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

db.Coupon.insert(...)
MongoDB shell中的

并不能提供您希望在Rails代码中找到的内容。

就Neil关于$exists与明确nil检查的评论而言,我认为您确实需要nil(MongoDB中的AKA null)检查。在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,没有nnull具有明确的n值,非{{{ 1}} null的值。

然后我们可以看到像n这样的Mongoid查询之间的区别:

:n => nil

> 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的文档。这两种情况都会发生在Mongoid上,除非您小心拨打nil而不是分配remove_attribute,并且这两种情况都意味着"没有到期日期"。

相关问题