# Best way to do a wizard that creates a set of objects?

**URL:** <https://www.ruby-forum.com/t/best-way-to-do-a-wizard-that-creates-a-set-of-objects/112173>\
**Category:** Rails\
**Created:** [August 16, 2007, 2:20am UTC](https://www.ruby-forum.com/t/best-way-to-do-a-wizard-that-creates-a-set-of-objects/112173 "2007-08-16T02:20:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 16, 2007, 2:20am UTC](https://www.ruby-forum.com/t/best-way-to-do-a-wizard-that-creates-a-set-of-objects/112173/1 "2007-08-16T02:20:41Z")

</div>

I have a multi-step user interaction to create a “parent” object that  
has a few more associated with it. I need to be able to let the user  
go back and forth between the pages so they can make corrections and  
such.

At first, I just started creating the objects (and saving to the db)  
from the first page on, but this seemed problematic in that I didnt  
want to have to cleanup the objects (and the db) if the user cancelled  
the process before completion.

So, I thought I’d store intermediate values in the session and then  
create and associate the various objects only at the end.

I’m not sure this approach is the best because it may be a use of  
session that isn’t really right and I lose the step by step validation  
(though I think there are other means of validating other than what  
gets performed by the callbacks… right?).

One thing I run into is that this approach needs to associate the form  
fields with something and if I just ref the session variables, it  
doesnt seem to work. Also, in the case that the user goes backwards in  
the wizard, I need to fill the form fields with the previously entered  
values.

Is there something obvious I’m missing here? Have I made this too  
complicated?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 16, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/best-way-to-do-a-wizard-that-creates-a-set-of-objects/112173/2 "2007-08-16T14:23:39Z")

</div>

lunaclaire wrote:

> I’m not sure this approach is the best…

Basically, you have a choice between storing them in the session or  
cleaning up a database when the user cancels the multistep operation. I  
prefer to store the partially-complete objects in the session –  
session[:model\_name][:field\_name]

> …because it may be a use of  
> session that isn’t really right and I lose the step by step validation  
> (though I think there are other means of validating other than what  
> gets performed by the callbacks… right?).

You can make a model instance any time and assign the attributes you  
have for that model in the session, something like:

@model = ModelName.new  
@model.attributes = session[:model\_name]

The check its validity like this –

do\_stuff\_for\_invalid\_model unless @model.valid?

Make sure your models are all valid by the time you get to the last step  
of the process, then (after you make your model and assign its  
attributes from the session)…

@model.save\_with\_validation(false)

–Al Evans

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 16, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/best-way-to-do-a-wizard-that-creates-a-set-of-objects/112173/3 "2007-08-16T21:36:01Z")

</div>

Thanks for the reply, Al.

I did decide to go with storing everything in the session and then  
clearing when the user completes or cancels and I think that leaves  
things in a clear state and is close enough “to the rails way”.

I didnt know about those validate methods and they may work fo rme if  
I build any part of the models along the way, but I’m actually not  
doing that yet until the end, so I may still be faced with the  
question of how to apply validations and catch a problem while the  
user is still on the interim pages because I would want them to  
correct something before moving to the next page…

It’s similar to another problem I’ve faced and not figured out yet  
which is how do I “validate” other forms that aren’t and wont be tied  
to a model? The example is a form to send an email and I want to make  
sure the addrs is valid.

Is there a general technique I’ve missed on validating forms not tied  
to models?

Thx again and TIA on any further help…
