该方法在开发中有效,但在生产中不起作用Rails MongoDB [英] 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
输入优惠券后,这在开发中效果很好.但是在生产中它不起作用.我使用MongoDB shell创建如下的优惠券.
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.
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日期时.在开发中,它找到了优惠券并开始工作,但在生产中,它一直没有找到优惠券.这只是我的控制器方法,仅供参考.
It seems that the problem is when the Coupon checks the expires_at date. 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.
编辑 我以为问题出在日期上,但是如果我删除了日期查询,它仍然无法在生产中使用.我很困惑为什么这不能在生产中工作.它使用MongoDB 3.0.10和Mongoid 5.1.0 gem
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
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
.那可以解释原因:
If you have a Coupon
Mongoid model then the collection in the MongoDB shell would be db.coupons
. That would explain why:
db.Coupon.insert(...)
MongoDB shell中的
没有提供您期望在Rails代码中找到的内容.
in the MongoDB shell isn't providing what you're expecting to find in your Rails code.
就Neil关于$exists
与显式nil
检查的评论而言,我认为您确实希望进行nil
(在MongoDB中也称为null
)检查.在MongoDB shell中考虑这一点:
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. Consider this in the 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
,具有n
的显式null
值和具有n
的非null
值的文档.
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 => nil
之类的Mongoid查询之间的区别:
Then we can see the difference between Mongoid queries like :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
,并且这两种情况都表示无有效期限".
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
. 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".
这篇关于该方法在开发中有效,但在生产中不起作用Rails MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!