满足最低订单后处理订单 - Rails 3
我的应用程序有一些交易,一旦达到最低订单金额,就会下订单。目前,我已经设置了订单,订单提交后立即下达,这一切正常。
现在我需要更改流程,以便在满足交易的最小订单数后处理订单。
我应该为此使用delayed_job还是一个cron进程?
这是我正在考虑遵循的过程。
延迟订单直至满足订单最小值
- 查找仍处于活动状态的交易
- 然后查找满足最小订单数量的交易
- 对于每个交易订单,循环遍历并处理每个订单的捕获。
- 发送订单确认信息。
我的订单模型购买方法
def purchase
response = OrderTransaction.gateway.purchase(order_amount, credit_card, options)
transactions.create!(:action => "purchase", :amount => order_amount, :response => response)
response.success?
end
我的订单控制器中的创建方法。
def create
@member = current_member
@order_deal = Deal.find(params[:deal_id])
@order = @order_deal.orders.build(params[:order])
@order.member_id = current_member.id
@order.ip_address = request.remote_ip
@deal = @order_deal
if @order.save
if @order.purchase
MessageMailer.order_receipt(@order, @member).deliver
render :action => "success"
flash[:notice] = "Successfully created order."
else
render :action => "new"
end
else
render :action => 'new'
end
end
我正在 Heroku 上运行这个应用程序。任何帮助表示赞赏。
My app has deals where orders are made once a minimum order amount is met. Currently I have setup orders that that orders are placed right after the order is submitted which is all working.
Now I need to change the process so that orders are processed once the minimum order number for the deal is met.
Should I use delayed_job for this or maybe a cron process?
Here is the process I was thinking of following.
Delaying Orders Until Orders minimum is met
- Find deals that are still active
- Then find deals where minimum number of orders are met
- For each deals orders, loop through and process capture on each order.
- Send confirmation of orders.
My order model purchase method
def purchase
response = OrderTransaction.gateway.purchase(order_amount, credit_card, options)
transactions.create!(:action => "purchase", :amount => order_amount, :response => response)
response.success?
end
My create method in my orders controller.
def create
@member = current_member
@order_deal = Deal.find(params[:deal_id])
@order = @order_deal.orders.build(params[:order])
@order.member_id = current_member.id
@order.ip_address = request.remote_ip
@deal = @order_deal
if @order.save
if @order.purchase
MessageMailer.order_receipt(@order, @member).deliver
render :action => "success"
flash[:notice] = "Successfully created order."
else
render :action => "new"
end
else
render :action => 'new'
end
end
I'm running this application on Heroku. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,基本上是一群人必须先下订单才能有人得到交易,就像 groupon 一样。如果是这样,一种更简单的方法可能是在每次下新订单时检查是否已下达最小订单数。因此,假设最少有两个订单,您下了第一个订单,您的订单将记录到数据库但未处理,然后我下订单。当我的订单被放置在您的应用程序中时,它可能会通过 before_save 在模型层中触发一个条件,然后应用程序会提取该交易的所有订单并处理它们。这种实现很好,因为您不必不断检查辅助进程(延迟作业)来查看是否满足最小值。一旦达到阈值,延迟作业可能有助于处理所有订单。
这是模型的一些示例代码(理论上您也可以在控制器中执行此操作)
If I understand correctly basically a bunch of people have to place an order before anyone gets the deal, like groupon. If so, a simpler way might be simply to check if the minimum number of orders have been placed every time a new order is placed. So say the minimum is two orders, you place the first and your order is logged to the db but not processed, then I place my order. When my order is placed your application it triggers a condition, probably in the model layer via before_save, and then the app pulls all of the orders for the deal and processes them. This implementation is nice because you don't have to constantly be checking with a secondary process (delayed job) to see if the minimum has been met. Delayed Job might be helpful for processing all of the orders once the threshold is met.
Here is some sample code for a model (you could also do it in the controller in theory)
最低订单金额是指订单上的美元价值吗?如果是这样,我只会使用满足这些条件的验证。假设最小值是 20,你可以这样做
By minimum order amount, do you mean a dollar value on the order? If so, I would just use a validation that meets those conditions. Let's say the minimum is 20, you could do