如何在aasm事件上​​返回值?

时间:2010-12-10 07:09:34

标签: ruby-on-rails aasm

如何让aasm事件返回布尔值以外的值?我正在使用aasm 2.2.0

E.g。有一个MusicPlayer模型,可以在启动时随机播放歌曲

aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly and return the song object
end

现在,如果我想在启动播放器时返回当前播放的歌曲,我该如何通过'play_song'方法进行播放?

1 个答案:

答案 0 :(得分:0)

你做不到。返回状态用于指示转换是否是可疑的。但我很好奇你有什么用例你需要它。

但是你可以包装状态转换并使用由play_song方法设置的变量,如下所示:

aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly
  @song = ...
end

def shuffle
  if start
    @song
  end
end
相关问题