Active Record回调在使用STI的类中生成“未定义的方法”错误

时间:2015-07-11 05:50:17

标签: ruby-on-rails passenger

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

  

InventoryOrder的未定义方法`before_save':Class   (NoMethodError)

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

以下是InventoryOrder类:

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 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

谢谢, 埃里克

1 个答案:

答案 0 :(得分:1)

在做了一些挖掘之后,在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)定义模块而不将它包装在类中,如下所示:

module OrderProcessingInfo
  extend ActiveSupport::Concern

  included do

  end
end

进行一些挖掘以找到它的底部,但希望这可能会帮助其他人。