最小订单满足后处理订单 - Rails 3 [英] Processing orders after minimum order met - Rails 3

查看:180
本文介绍了最小订单满足后处理订单 - Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式有交易,一旦达到最低订单金额,即可订购。目前,我已经设置订单,在订单提交后立即下订单,这是所有工作。



现在我需要更改过程,以便订单处理一次



我应该为此使用delayed_job还是使用cron流程?






$ b $ b
  • 查找仍然有效的交易

  • 然后在满足最低订单数量的情况下查找交易

  • 对于每个交易订单,循环

  • 我的订单模式购买方法

      def purchase 
    response = OrderTransaction.gateway.purchase(order_amount,credit_card,options)
    .create!(:action =>purchase,:amount => order_amount,: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] =成功创建订单。
    else
    render:action => new
    end
    else
    render:action => 'new'
    end
    end

    我在Heroku上运行此应用程序。

    解决方案

    如果我理解正确,一群人必须在任何人获得交易之前下订单,像groupon。如果是这样,更简单的方法可能是简单地检查每次新订单被放置时是否放置了最小数量的订单。所以说最小是两个订单,你放置第一个,你的订单记录到db,但没有处理,然后我下订单。当我的订单放置你的应用程序时,它触发一个条件,可能在模型层通过before_save,然后应用程序拉取所有订单的交易,并处理它们。这个实现是很好的,因为你不必不断地检查一个辅助进程(延迟工作),看看是否已满足最低要求。延迟作业可能有助于在满足阈值后处理所有订单。



    以下是模型的一些示例代码(您也可以在理论上在控制器中执行)

      before_save:check_if_minimum_has_been_met 

    def check_if_minimum_has_been_met

    如果deal.minimum_orders< dealerorders + 1#计算当前订单
    #execute延迟的工作/订单处理
    else
    #do nothing ....
    end
    end


    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

    1. Find deals that are still active
    2. Then find deals where minimum number of orders are met
    3. For each deals orders, loop through and process capture on each order.
    4. 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.

    解决方案

    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)

    before_save :check_if_minimum_has_been_met
    
    def check_if_minimum_has_been_met
    
      if deal.minimum_orders < deal.orders + 1 #to count the current order
        #execute the delayed job/order processing
      else
        #do nothing....
      end
    end
    

    这篇关于最小订单满足后处理订单 - Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆