使用 STI 的 类 的 Active Record 回调在生产中抛出 "undefined method" 错误

Active Record callbacks throw "undefined method" error in production with classes using STI

我的应用程序中有很多实例,我使用单个 table 继承,并且在我的开发环境中一切正常。但是当我发布到生产环境(使用乘客)时,出现以下错误:

undefined method `before_save' for InventoryOrder:Class (NoMethodError)

为什么这会在我的开发环境中工作而不在生产环境中工作?两者都使用 Rails 4.2 和 Ruby 2.1.5。这可能是乘客的问题吗?

这是 InventoryOrder class:

class InventoryOrder < Order

    def self.model_name
        Order.model_name
    end

    before_save :ensure_only_feed_types

    def ensure_only_feed_types
        order_products.each do |op|
            if !ProductTypes::is_mix?(op.product_type.type)
                raise Exceptions::FailedValidations, _("Can't have an inventory order for anything but mixes")
            end
        end
    end

    def self.check_if_replenishment_order_is_needed(product_type_id)

        prod_type = ProductType.find(product_type_id)

        return if prod_type.nil? || prod_type.min_system_should_have_on_hand.nil? || prod_type.min_system_should_have_on_hand == 0                

        amount_free = Inventory::inventory_free_for_type(product_type_id)

        if prod_type.min_system_should_have_on_hand > amount_free
            if prod_type.is_mix?
                InventoryOrder::create_replenishment_order(product_type_id, prod_type.min_system_should_have_on_hand - amount_free)
            else
                OrderMoreNotification.create({subject: "Running low on #{prod_type.name}", body: "Should have #{prod_type.min_system_should_have_on_hand} of unreserved #{prod_type.name} but only #{amount_free} is left"})
            end
        end

    end

    def self.create_replenishment_order(product_type_id, amount)

        # first check for current inventory orders
        orders = InventoryOrder.joins(:order_products).where("order_products.product_type_id = ? and status <> ? and status <> ?", product_type_id, OrderStatuses::ready[:id], OrderStatuses::completed[:id])

        amount_in_current_orders = orders.map {|o| o.order_products.map {|op| op.amount }.sum }.sum
        amount_left_to_add = amount - amount_in_current_orders

        if amount_left_to_add > 0
            InventoryOrder.create({pickup_time: 3.days.from_now, location_id: Location::get_default_location.id, order_products: [OrderProduct.new({product_type_id: product_type_id, amount: amount_left_to_add})]})
        end     

    end

    def self.create_order_from_cancelled_order_product(order_product)
        InventoryOrder.create({
            pickup_time: DateTime.now.change({ min: 0, sec: 0 }) + 1.days,
            location_id: Location::get_default_location.id,
            order_products: [OrderProduct.new({
                product_type_id: order_product.product_type_id,
                feed_mill_job_id: order_product.feed_mill_job_id,
                ration_id: order_product.ration_id,
                amount: order_product.amount
              })],
            description: "Client Order for #{order_product.amount}kg of #{order_product.product_type.name} was cancelled after the feed mill job started."
        })
    end

end

这是它的父级 class:

class Order < ActiveRecord::Base
  #active record concerns
  include OrderProcessingInfo

  belongs_to :client
  belongs_to :location
  has_many :order_products
  before_destroy :clear_order_products

  after_save :after_order_saved
  before_save :on_before_save

  accepts_nested_attributes_for :order_products, allow_destroy: true

  after_initialize :init #used to set default values  

  validate :client_order_validations

  def client_order_validations
    if self.type == OrderTypes::client[:id] && self.client_id.nil?
      errors.add(:client_id, _("choose a client"))
    end

  end  
...

end

谢谢, 埃里克

在进一步挖掘之后,在 Roman 的评论的帮助下,我发现这个问题是我对 ActiveRecord::Concerns 使用旧约定的结果,该约定在 windows 上运行良好但不是在基于 unix 的系统上。

根据 this RailsCasts,您可以这样定义您的顾虑:

在../models/concerns/order/order_processing_info.rb

class Order
 module OrderProcessingInfo
  extend ActiveSupport::Concern

  included do

  end
  ...
end

但根据 this 定义问题的正确方法是

1) 将其放入 ../models/concerns/[FILENAMEHERE] 而不是 ../models/concerns/[CLASSNAMEHERE]/[FILENAMEHERE]

2) 定义模块而不将其包装在 class 中,如下所示:

module OrderProcessingInfo
  extend ActiveSupport::Concern

  included do

  end
end

进行了一些挖掘以找到它的底部,但希望这可能对那里的其他人有所帮助。