2011-12-23 42 views
0

我使用pluginaweek/state_machine寶石 我需要驗證對象從用戶提交表單來驗證對象,但只有過渡到一定前州。其他州不需要驗證。從狀態機的形式來PARAMS轉換之前某些國家

我有:

class Invoice < ActiveRecord::Base 

    state_machine :state, :initial => :draft do 
    before_transition :draft => :emited, :do => :check_date 

    state :emited 
    state :draft 
    end 

    def check_date 
    if params[:date] < Time.now 
    false 
    end 
    end 

end 

但PARAMS哈希不在型號。有辦法做我需要的嗎?

回答

0
class Invoice < ActiveRecord::Base 
    state_machine :state, :initial => :draft do 
    state :draft 

    state :emited do 
     validate :validate_date 
    end 
    end 

    def validate_date 
    errors.add(:date, "in invalid") if params[:date] < Time.now 
    end 
end 

希望這會有所幫助