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

在生产环境中的应用程序中出现随机错误。当我重新启动服务器时,问题消失了一段时间,然后又浮出水面。

这是错误

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

故障排除

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

根据@lifius,我运行了以下命令:

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控制台中,查看受影响的模型。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未定义的方法NoMethodError

NoMethodError未定义的方法

NoMethodError:未定义的方法

NoMethodError:未定义的方法`with_deleted`

rails NoMethodError:未定义的方法“酒店”

NoMethodError:HTTParty的未定义方法“提交”

Sidekiq:NoMethodError:未定义的方法“执行”

NoMethodError:未定义的方法“start_with?”

注入:NoMethodError:未定义的方法“ <<”::Symbol

ruby - NoMethodError:未定义的方法“状态”

NoMethodError:未定义的方法“创建”

NoMethodError:main:Object的未定义方法“ on”

NoMethodError: []:Array 的未定义方法“错误”

NoMethodError:RSpec 的未定义方法“get”

NoMethodError:#<Model>的未定义方法“ pluck”

NoMethodError:nil:NilClass的未定义方法“ +”

nil:NilClass <NoMethodError>的未定义方法'[]'

NoMethodError:未定义的方法“ where”

测试NoMethodError:未定义的方法

NoMethodError: #<Class> 的未定义方法“类型”

Ruby 2.0.0:未定义的方法“已保险?” 对于main:Object(NoMethodError)

NoMethodError:未定义的方法reset_sent_at Hartle教程

NoMethodError:未定义的方法和#ActiveRecord :: Relation []

Rails NoMethodError(52:Integer 的未定义方法“回复”):

NoMethodError未定义的方法`',用于nil:NilClass

未定义的方法“ +”,用于nil:NilClass(NoMethodError)-Ruby

Ruby / calabash:2:Fixnum的未定义方法“ each”(NoMethodError)

NoMethodError未定义的方法format用于`modify`:String

Rails DateTime-NoMethodError(nil:NilClass的未定义方法“ []”)