2011-10-25 30 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 

是否有一些很好的方式整齊地包裝這些如果塊?

回答