1

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)
Besi
  • 22,579
  • 24
  • 131
  • 223
  • 1
    Could you please provide us with the output of `ActiveRecord::Base.descendants.find {|e| e.singleton_methods.include?(:locked)}` (Make sure to invoke `Rails.application.eager_load!` first. http://stackoverflow.com/a/10712838/2286990 )? It seems like some model contains `scope :locked` or `self.locked` – lifus Jan 31 '15 at 19:41
  • @lifus I do have a `scope` named `locked`. I will remove this and I guess this will resolve the issue. You may post this as an answer and I will happily accept it. – Besi Jan 31 '15 at 20:10

1 Answers1

3

You may execute the following:

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

in the rails console and see affected models.

lifus
  • 8,074
  • 1
  • 19
  • 24