Search code examples
ruby-on-railsrubyruby-on-rails-2

NoMethodError (undefined method `new_payment_model' for nil:NilClass) in ROR Version 2


I have a face error in ROR version 2, NoMethodError (undefined method `new_payment_model' for nil:NilClass) here is my code

class Authorization < ActiveRecord::Base
  belongs_to :franchisee
  belongs_to :deleted_by, :class_name => 'User', :foreign_key => :deleted_by
  belongs_to :created_by, :class_name => 'User', :foreign_key => :created_by
  has_many   :transactions
  validates_presence_of :notify_email
  validates_presence_of :activation_price, :expires_on
  validates_length_of :notify_email, :maximum => 255
  validates_format_of :notify_email, :with => EMAIL_REGEX
  validates_numericality_of :activation_price, :greater_than => 0.0, :unless => Proc.new {|a| a.franchisee.new_payment_model}
end

Solution

  • The issue happens in validation proc in a.franchisee.new_payment_model.

    I would assume, franchisee might be nil. I believe this would fix the issue:

    validates_numericality_of
      :activation_price,
      :greater_than => 0.0,
      :unless => Proc.new { |a| a.franchisee.nil? || a.franchisee.new_payment_model}
    end