ROR 版本 2 中的 NoMethodError(nil:NilClass 的未定义方法“new_payment_model”)
NoMethodError (undefined method `new_payment_model' for nil:NilClass) in ROR Version 2
我在 ROR 版本 2 中遇到错误,NoMethodError(nil:NilClass 的未定义方法 `new_payment_model')这是我的代码
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
问题发生在 a.franchisee.new_payment_model
的验证过程中。
我假设,franchisee
可能是 nil
。我相信这会解决问题:
validates_numericality_of
:activation_price,
:greater_than => 0.0,
:unless => Proc.new { |a| a.franchisee.nil? || a.franchisee.new_payment_model}
end
我在 ROR 版本 2 中遇到错误,NoMethodError(nil:NilClass 的未定义方法 `new_payment_model')这是我的代码
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
问题发生在 a.franchisee.new_payment_model
的验证过程中。
我假设,franchisee
可能是 nil
。我相信这会解决问题:
validates_numericality_of
:activation_price,
:greater_than => 0.0,
:unless => Proc.new { |a| a.franchisee.nil? || a.franchisee.new_payment_model}
end