I get an error in my application on production envinronment at a random rate. When I restart the server the problem disappears for some time and then resurfaces.
This is the error
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'
The Codesnipped looks like this:
@order.product_option = ProductOption.find_by_identifier(params[:product]) rescue ProductOption.first
For explanation. This snipped pre-selects the product option in the front-end.
The error occors in other areas and also related with the ProductOption
model.
The model product_option
looks like this:
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
This is 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
I am using Rails v3.2.18
Troubleshooting
When I did some research I came accross this rails-issue #7421. But the issue was closed and declared as not being a bug.
According to @lifius i ran the following command:
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)