3
我有一個Participant模型,其中包含state_machine last_action。我想根據角色屬性設置last_action的初始值。如果角色是「導師」,則初始值是「值1」,而如果角色是「學生」,則初始值是「值2」。以其他屬性爲條件設置state_machine初始狀態
我該怎麼做? (我在Rails 3.0中使用了state_machine gem)。 謝謝。
我有一個Participant模型,其中包含state_machine last_action。我想根據角色屬性設置last_action的初始值。如果角色是「導師」,則初始值是「值1」,而如果角色是「學生」,則初始值是「值2」。以其他屬性爲條件設置state_machine初始狀態
我該怎麼做? (我在Rails 3.0中使用了state_machine gem)。 謝謝。
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
乾杯