我在ROR版本2中有一个面错误,NoMethodError(未定义的方法`new_payment_model'代表nil:NilClass)这里是我的代码
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
答案 0 :(得分:1)
问题发生在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