NoMethodError(未定义的方法“锁定”

NoMethodError (undefined method `locked'

我的生产环境应用程序以 运行dom 速率出现错误。当我重新启动服务器时,问题会消失一段时间然后重新出现。

这是错误

NoMethodError (undefined method `locked' for #<Class:0x00000006776a40>):
  app/controllers/orders_controller.rb:29:in `rescue in new'
  app/controllers/orders_controller.rb:29:in `new'

Codesnipped 看起来像这样:

@order.product_option = ProductOption.find_by_identifier(params[:product]) rescue ProductOption.first

求解释。此片段在前端预选产品选项。

错误发生在其他地方,也与ProductOption模型有关。

模型 product_option 看起来像这样:

class ProductOption < ActiveRecord::Base
  attr_accessible :identifier, :price, :servings, :title

  before_destroy :check_for_deps

  has_many :users
  has_many :orders
  belongs_to :product

  attr_accessible :product_id, :product, :price, :identifier, :servings, :color

  validates_presence_of :identifier, :price, :product
  validates_numericality_of :price, greater_than_or_equal_to: 1
  validates_numericality_of :servings, greater_than_or_equal_to: 1

  default_scope order('products.position, servings').includes(:product)

  def title
    I18n.t 'order_form.product_option_title',
           recipe_count: self.product.recipe_count,
           product_title: self.product.title,
           servings: self.servings
  end

  def subtitle
    self.product.subtitle
  end

  def pretty_price
    '%.2f' % self.price
  end

  def check_for_deps
    if users.count > 0
      errors.add(:base, I18n.t('model.validation.product_has_still_objects_assigned'))
      return false
    end

    if orders.count > 0
      errors.add(:base, I18n.t('model.validation.product_has_still_objects_assigned'))
      return false
    end
  end

end

这是product.rb:

class Product < ActiveRecord::Base

  before_destroy :check_for_options
  acts_as_list

  translates :title, :subtitle, :description
  active_admin_translates :title, :subtitle, :description do
    validates_presence_of :title
  end

  attr_accessible :discount, :remarks, :title, :description, :subtitle, :product_options, :product_option_ids, :recipe_count

  validates_presence_of :title

  has_many :recipe_assignments
  has_many :deliveries, through: :recipe_assignments

  has_many :orders
  has_many :product_options

  default_scope order('position ASC')

  private

  def check_for_options
    if product_options.count > 0
      errors.add(:base, I18n.t('model.validation.product_has_still_objects_assigned'))
      return false
    end
  end

end

我正在使用 Rails v3.2.18

疑难解答

当我做一些研究时,我遇到了这个 rails-issue #7421。但该问题已关闭并声明为不是错误。

根据@lifius i 运行 以下命令:

culprit = :locked
ActiveRecord::Base.descendants.find {|e| e.singleton_methods.include?(culprit)}

# Result
Delivery(id: integer, delivery_date: date, remarks: text, created_at: datetime, updated_at: datetime, status: string)

您可以执行以下操作:

Rails.application.eager_load!
ActiveRecord::Base.descendants.find {|e| e.singleton_methods.include?(:locked)}

在 rails 控制台中查看受影响的模型。