# How can I save values before the record is created?

**URL:** <https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681>\
**Category:** Rails\
**Created:** [May 5, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681 "2008-05-05T14:54:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 5, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/1 "2008-05-05T14:54:09Z")

</div>

I’ve been doing a lot of work with pop up windows and have now come to  
the “it all works, but this” point in my application.

I have an order application. With that I have a pop up window that  
allows users to do some dragging and dropping of items for a portion of  
the order. The problem is that the pop up uses the order id. The items  
selected in the pop up window are saved back to the order record as a  
user drags and drops items. Obviously works great with existing orders,  
but blows up with new orders.

I am saving everything back to the order record. There is not a  
separate table to hold the list for this item of the order.

Right now the I open the pop-up window using link\_to and sending in the  
id of the order. If it’s a new order, I get “Couldn’t find Order  
without an ID”.

Since the Order ID is integral to everything working, what are my  
options? Do I check for the order ID and if it’s not provided, start a  
new order and save it to the database?

Or do I add a route for the model/pop\_up?

Right now my route is map.resources :model, :collection =\> [“pop\_up”]

Or is this a bigger, uglier problem that really can’t be attacked with  
this design? If that’s the case, some ideas for how to fix, without  
starting all over would be appreciated.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 5, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/2 "2008-05-05T15:31:29Z")

</div>

On Mon, May 5, 2008 at 5:54 AM, Becca G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Right now the I open the pop-up window using link\_to and sending in the  
> id of the order. If it’s a new order, I get “Couldn’t find Order  
> without an ID”.
> 
> Since the Order ID is integral to everything working, what are my  
> options? Do I check for the order ID and if it’s not provided, start a  
> new order and save it to the database?

That sounds like it would work.

Alternatively, create a new order when the page that launches the  
popup is called (or somewhere earlier in the process, like when an  
item is first added to the cart).

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 5, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/3 "2008-05-05T15:37:26Z")

</div>

> Right now the I open the pop-up window using link\_to and sending in the  
> id of the order. If it’s a new order, I get “Couldn’t find Order  
> without an ID”.
> 
> Since the Order ID is integral to everything working, what are my  
> options? Do I check for the order ID and if it’s not provided, start a  
> new order and save it to the database?  
> That’s an option. Or I’d just create it arbitrarily before the pop  
> up. Save grief 🙂  
> Having a few extras around isn’t a problem.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 5, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/4 "2008-05-05T21:40:36Z")

</div>

From an outsider’s perspective it sounds like you have two different  
but similar things going on:

- Order#edit with drag-and-drop to add and remove items from an  
existing order
- Order#new with drag-and-drop to set the initial list of items on the  
order

The fact that these two distinct actions have very, very similar UIs  
is just clouding what’s going on.

With that in mind I can think of two different approaches:

1. Have distinct new and edit views that pull in the drag-and-drop  
functionality via partials.
2. Have one popup view but pass in the url and http verb for the form  
postback. Here you’d give the appropriate urls for create or update.

On May 5, 11:27 am, Becca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 6, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/5 "2008-05-06T17:19:13Z")

</div>

AndyV wrote:

> With that in mind I can think of two different approaches:
> 
> 1. Have distinct new and edit views that pull in the drag-and-drop  
> functionality via partials.
> 2. Have one popup view but pass in the url and http verb for the form  
> postback. Here you’d give the appropriate urls for create or update.

Thanks for your tips. I will probably go with option 2.

But just to look at one other option, does anyone know if it’s possible  
to use RoR and Drag and Drop without going to the server to update  
lists? I keep thinking that there must be some way to do this using  
Prototype or Javascript or something. In RoR, it looks like I might  
need to take a trip to the server, with the :url option, but is it  
possible to just keep it all on the client side?

Thanks for everyone’s thoughts.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 6, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/6 "2008-05-06T21:21:03Z")

</div>

Yes. You could put a hidden field on the form that maps to the  
attribute where you store the string of ids. When you declare the  
droppable, add an onDrop function that pushes the id of the item onto  
the end of the string. I’m not sure what you’d hook for the remove  
(sure wish they’d fix the [script.aculo.us](http://script.aculo.us) wiki…).

Good luck!

On May 6, 11:19 am, Becca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [May 5, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/how-can-i-save-values-before-the-record-is-created/136681/7 "2008-05-05T17:27:02Z")

</div>

Roger P. wrote:  
Or I’d just create it arbitrarily before the pop

> up. Save grief 🙂

As always, thanks for the great ideas.

The pop-up is opened with link\_to. When would the order be created? I  
don’t think that I really want to create the order when the new order  
window is opened, so I think that I’d want to create it after that.

My thought is to put this in the controller that associated with the  
pop-up window opening and it’s params.

Also, I know this is super basic, but could someone point me to some  
syntax that would create the new order and then grab the ID after the  
new order is saved in the db. Order.create just creates a nil value for  
ID.
