2011-10-25 33 views
2

使用state_machine时,如何有条件地验证下面的字段?Rails 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 

这似乎只是忽略,如果条件和验证状态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 

是否有一些很好的方式整齐地包装这些如果块?

回答