Processing orders after minimum order met - Rails 3

与世无争的帅哥 提交于 2020-01-23 21:56:07

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/6171711/processing-orders-after-minimum-order-met-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!