只有当 user_id 字段为空时,我才想验证注释字段?
我试过没有 lambda:
validates_presence_of :note, :unless => user_id?
我尝试了一种方法:
validates_presence_of :note, :unless => user_is_present?
def user_is_present?
self.user_id.present?
end
我尝试了一个 lambda:
validates_presence_of :note, :unless => lambda { self.user_id.present? }
我尝试了一个 Proc
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"
Trial & Error 确认 nil 值是 user_id ...未验证且不应失败。
- 第一个问题是我的尝试有什么错误吗?
- 第二个问题是我怎样才能做到这一点?