我有一個模型依賴於state_machine來管理其不同的狀態,一個特定的事件需要一個before_transition,因爲它需要在轉換前建立一個連接表,不幸的是它不起作用。before_filter not working in state_machine :(
class DocumentSet < ActiveRecord::Base
state_machine :state, :initial => :draft do
# Callbacks
before_transition :on=>:submit, :do=>:populate_join_table
# States
state :draft
state :submitted
# Events
event :submit do transition :draft=>:submitted, :if=>:can_submit? end
end
def populate_join_table
puts '::::::::: INSIDE POPULATE_JOIN_TABLE'
end
def can_submit?
raise "Document Set has no Document(s)" if self.document_versions.blank?
true
end
現在,當我做DocumentSet.submit,它實際上從來沒有因爲它評估can_submit?假進入populate_join_table。
我缺少什麼?