[英]Method works in development but not production Rails MongoDB
I have a Coupon class and I want my app to check and see how many counts are left on the coupon and if the date for the coupon has expired. 我有优惠券课程,我希望我的应用程序检查并查看优惠券上还有多少计数,以及优惠券的日期是否已过期。 I have the following method in my class to check both of these. 我的课上有以下方法检查这两个方法。
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
This works fine in development when I enter a coupon. 输入优惠券时,这在开发中效果很好。 But in production it does not work. 但是在生产中它不起作用。 I use my MongoDB shell to create a coupon as follows. 我使用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'})
It seems that the problem is when the Coupon checks the expires_at date. 看来问题在于优惠券何时检查expires_at日期。 In development it finds the coupon and works but in production it keeps not finding the coupon. 在开发中,它找到了优惠券并开始工作,但在生产中,它一直没有找到优惠券。 Just for good measure here is my controller method for this. 这只是我的控制器方法,仅供参考。
EDIT I thought the issue was with the date but if I remove the date query it still does not work in production. 编辑我以为问题与日期有关,但是如果我删除日期查询,它仍然无法在生产中使用。 I am confused why this wont work in production. 我很困惑为什么这将无法在生产中工作。 It is using MongoDB 3.0.10 and mongoid 5.1.0 gem 它使用的是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
If you have a Coupon
Mongoid model then the collection in the MongoDB shell would be db.coupons
. 如果您有Coupon
Mongoid模型,则MongoDB Shell中的集合将为db.coupons
。 That would explain why: 那可以解释为什么:
db.Coupon.insert(...)
in the MongoDB shell isn't providing what you're expecting to find in your Rails code. 在MongoDB Shell中,没有提供您期望在Rails代码中找到的内容。
As far as Neil's comment about $exists
versus explicit nil
checks goes, I think you really do want nil
(AKA null
inside MongoDB) checks. 就Neil关于$exists
和显式nil
检查的评论而言,我认为您确实希望nil
(在MongoDB中为AKA null
)检查。 Consider this in the MongoDB shell: 在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") }
So we have a collection with documents that have n
, don't have n
, have explicit null
values for n
, and non- null
values for n
. 因此,我们有一个包含了文档的集合n
,没有n
,有明确的null
的值n
非,以及null
的值n
。
Then we can see the difference between Mongoid queries like :n => nil
: 然后我们可以看到Mongoid查询之间的区别,如:n => nil
:
> db.models.find({ n: null })
{ "_id" : ObjectId("571546e7ce2934dadf37947b"), "n" : null }
{ "_id" : ObjectId("571546ecce2934dadf37947c") }
and :n.exists => true
(AKA :n => { :$exists => true }
): 和: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 }
and :n => { :$exists => false }
: 和:n => { :$exists => false }
:
> db.models.find({ n: { $exists: false } })
{ "_id" : ObjectId("571546ecce2934dadf37947c") }
So the :expires_at => nil
queries will find documents which don't have an expires_at
as well as documents where expires_at
was explicitly set to nil
. 因此:expires_at => nil
查询将查找没有expires_at
文档以及将expires_at
显式设置为nil
文档。 Both those cases will happen with Mongoid unless you're careful to call remove_attribute
instead of assigning a nil
and both cases mean "no expiry date". 这两种情况都将在Mongoid中发生,除非您小心地调用remove_attribute
而不是分配nil
并且这两种情况均表示“无有效期限”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.