StateMachine without AR Integration on an AR Model

60 views
Skip to first unread message

Rainer Kuhn

unread,
Mar 2, 2012, 7:00:07 AM3/2/12
to PluginAWeek: Talk
Hi Guys,

I really enjoy state_machine so far. I'm currently creating a
multistep form as seen in Railscast 217 http://railscasts.com/episodes/217-multistep-forms

Ryan Bates creates a whole bunch of instance methods that should
rather be handled by a state machine. But it's state should not be
saved in the database, since it's just handling new forms.

Is it possible to include a state machine on my model that does not
try to save the state automatically and just uses an instance variable
on the unsaved record?

Rainer

Rainer Kuhn

unread,
Mar 2, 2012, 10:22:19 AM3/2/12
to PluginAWeek: Talk
I think I might have found a way.

After reading through StateMachine::Integrations::ActiveRecord I
puzzled this together. Seems to pass the first few specs so far.

state_machine :form_step, :initial
=> :field_type_step, :use_transactions => false, :action => nil do

http://rdoc.info/github/pluginaweek/state_machine/master/frames

Rainer

On Mar 2, 12:00 pm, Rainer Kuhn <rai...@incutio.com> wrote:
> Hi Guys,
>
> I really enjoy state_machine so far. I'm currently creating a
> multistep form as seen in Railscast 217http://railscasts.com/episodes/217-multistep-forms

Aaron Pfeifer

unread,
Mar 6, 2012, 10:21:08 PM3/6/12
to pluginaw...@googlegroups.com
Hey Rainer -

Great question!  What you found is pretty much exactly what I would have recommended.  I don't *think* you actually need the :use_transactions option -- you should just be able to override the action (as you've done) and call it a day.

The one alternative is that you can completely turn off all of the ActiveRecord features or explicitly force state_machine to treat the integration like an ActiveModel integration which will avoid creating things like named scopes.  For example:

state_machine :form_step, :initial => :field_type_step, :integration => :active_model do
  ...
end

Hope this helps and best of luck!

Best,
Aaron
Reply all
Reply to author
Forward
0 new messages