Ruby on rails 3 rails 3 ruby 2条件验证未按预期工作
我只想在用户id字段为空时验证备注字段 我试过不用兰姆达:Ruby on rails 3 rails 3 ruby 2条件验证未按预期工作,ruby-on-rails-3,validation,conditional,ruby-2.0,Ruby On Rails 3,Validation,Conditional,Ruby 2.0,我只想在用户id字段为空时验证备注字段 我试过不用兰姆达: validates_presence_of :note, :unless => user_id? validates_presence_of :note, :unless => lambda { self.user_id.present? } 我尝试了一种方法: validates_presence_of :note, :unless => user_is_present? def user_
validates_presence_of :note, :unless => user_id?
validates_presence_of :note, :unless => lambda { self.user_id.present? }
我尝试了一种方法:
validates_presence_of :note, :unless => user_is_present?
def user_is_present?
self.user_id.present?
end
我试了一个兰姆达:
validates_presence_of :note, :unless => user_id?
validates_presence_of :note, :unless => lambda { self.user_id.present? }
我试过一个程序
validates_presence_of :note, :unless => Proc.new { |x| x.user_id.present? }
所有结果如下:
"Called id for nil, which would mistakenly be 8 -- if you really wanted the id of nil, use object_id"
试用和错误确认为nil值为用户id…未经验证且不应失败
validates :note, presence: true, unless: 'user_id'
您不需要定义任何方法