【发布时间】:2011-10-25 15:34:39
【问题描述】:
在使用 state_machine 时,如何通过以下方式有条件地验证字段?
state :unlit do
end
state :fire do
if is_big_fire?
validates_presence_of :big_log
end
if is_small_fire?
validates_presence_of :small_log
end
end
似乎只是忽略了 if 条件并验证状态 D 内的所有内容:
我想出的唯一种解决方案是
validates_presence_of :big_log, :if => Proc.new { |fire| fire.is_big_fire? }
但是如果有更多的验证,这会变得很疯狂。
validates_presence_of :big_log, :if => Proc.new { |fire| fire.is_big_fire? }
validates :fire_epicness_rating, :inclusion => { :in => %w(epic whowa RUNFORTHEHILLS) }, :if => Proc.new { |fire| fire.is_big_fire? }
etc
有没有一些很好的方法可以将它们整齐地包装在 if 块中?
【问题讨论】:
标签: ruby-on-rails validation activerecord state