gpt4 book ai didi

ruby-on-rails - NoMethodError(未定义方法 `locked'

转载 作者:太空宇宙 更新时间:2023-11-03 18:14:34 25 4
gpt4 key购买 nike

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

这是错误

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,我运行了以下命令:

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

关于ruby-on-rails - NoMethodError(未定义方法 `locked',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28254563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com