满足最低订单后处理订单 - Rails 3

发布于 2024-11-10 13:00:59 字数 1218 浏览 0 评论 0原文

我的应用程序有一些交易,一旦达到最低订单金额,就会下订单。目前,我已经设置了订单,订单提交后立即下达,这一切正常。

现在我需要更改流程,以便在满足交易的最小订单数后处理订单。

我应该为此使用delayed_job还是一个cron进程?

这是我正在考虑遵循的过程。

延迟订单直至满足订单最小值

  1. 查找仍处于活动状态的交易
  2. 然后查找满足最小订单数量的交易
  3. 对于每个交易订单,循环遍历并处理每个订单的捕获。
  4. 发送订单确认信息。

我的订单模型购买方法

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

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2024-11-17 13:00:59

如果我理解正确的话,基本上是一群人必须先下订单才能有人得到交易,就像 groupon 一样。如果是这样,一种更简单的方法可能是在每次下新订单时检查是否已下达最小订单数。因此,假设最少有两个订单,您下了第一个订单,您的订单将记录到数据库但未处理,然后我下订单。当我的订单被放置在您的应用程序中时,它可能会通过 before_save 在模型层中触发一个条件,然后应用程序会提取该交易的所有订单并处理它们。这种实现很好,因为您不必不断检查辅助进程(延迟作业)来查看是否满足最小值。一旦达到阈值,延迟作业可能有助于处理所有订单。

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

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

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
无边思念无边月 2024-11-17 13:00:59

最低订单金额是指订单上的美元价值吗?如果是这样,我只会使用满足这些条件的验证。假设最小值是 20,你可以这样做

validates :amount, :numericality => { :greater_than_or_equal_to => 20, if => :is_a_deal }
private
  def is_a_deal
    # deal logic here. Return true/false
  end

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

validates :amount, :numericality => { :greater_than_or_equal_to => 20, if => :is_a_deal }
private
  def is_a_deal
    # deal logic here. Return true/false
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文