繁体   English   中英

方法在开发中有效,但在生产中不起作用Rails MongoDB

[英]Method works in development but not production Rails MongoDB

我有优惠券课程,我希望我的应用程序检查并查看优惠券上还有多少计数,以及优惠券的日期是否已过期。 我的课上有以下方法检查这两个方法。

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 (在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 ,没有n ,有明确的null的值n非,以及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显式设置为nil文档。 这两种情况都将在Mongoid中发生,除非您小心地调用remove_attribute而不是分配nil并且这两种情况均表示“无有效期限”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM