Ruby on rails RoR:如何在每次订购后触发python脚本

Ruby on rails RoR:如何在每次订购后触发python脚本,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我是RoR的新手。我想在每个订单之后运行一个自定义任务。更具体地说,我想运行一个python脚本来更新另一个站点上相同产品的数量 所以我的问题是,如何在每次订单之后触发脚本 谢谢您可以添加一个。但这会阻碍 class OrdersController < ApplicationController after_filter :update_product_count, :only => :new_order def new_order ... end pr

我是RoR的新手。我想在每个订单之后运行一个自定义任务。更具体地说,我想运行一个python脚本来更新另一个站点上相同产品的数量

所以我的问题是,如何在每次订单之后触发脚本

谢谢

您可以添加一个。但这会阻碍

class OrdersController < ApplicationController
  after_filter :update_product_count, :only => :new_order

  def new_order
    ...
  end

  private
  def update_product_count
    system "python /path/to/update/script.py"
  end
end
class OrdersController:新订单
def新订单
...
结束
私有的
def更新产品计数
系统“python/path/to/update/script.py”
结束
结束
或者你可以在后台做(我更喜欢gem)

订单型号:

class Order < ActiveRecord::Base

  private

  def update_product_count order_id
    # find products in this order and pass them to python script
    products = Order.find(order_id).products.map(&:id)
    system "python /path/to/update/script.py #{products}"
  end
end
class OrdersController < ApplicationController

  def new_order
    order.new
    ...
    order.save!
    Order.delay.update_product_count(order.id)
  end
end
类顺序
订单管理员:

class Order < ActiveRecord::Base

  private

  def update_product_count order_id
    # find products in this order and pass them to python script
    products = Order.find(order_id).products.map(&:id)
    system "python /path/to/update/script.py #{products}"
  end
end
class OrdersController < ApplicationController

  def new_order
    order.new
    ...
    order.save!
    Order.delay.update_product_count(order.id)
  end
end
class OrdersController
您可以像ruby一样运行命令`python命令“”,请注意“”。

如果订单是ActiveRecord模型,则可以在创建每个订单后使用运行代码。例如:

class Order < ActiveRecord::Base
  after_create :update_count

  private

  def update_count
    system "python /path/to/script.py #{self.class.count}"
  end
end
类顺序
如果发布脚本可能需要很长时间,则需要生成一个后台作业来运行脚本。有许多Ruby库可以为您管理它;看一看,或者开始