2011-10-25 58 views
3

我有一个Participant模型,其中包含state_machine last_action。我想根据角色属性设置last_action的初始值。如果角色是“导师”,则初始值是“值1”,而如果角色是“学生”,则初始值是“值2”。以其他属性为条件设置state_machine初始状态

我该怎么做? (我在Rails 3.0中使用了state_machine gem)。 谢谢。

回答

0
before_validation :set_initial_last_action, :on => :create 

state_machine :last_action do 
    ... 
end 

private 

def set_initial_last_action 
    if role == 'Tutor' 
    self.last_action = 'value 1' 
    elsif role == 'Student' 
    self.last_action = 'value 2' 
end 
end 

干杯