Save Model On Nested Model Create

Hi all,

Having a model (Orders) and a nested one (Elements). Elements has a FK order_id (default null) specifying the the Orders belongs to.

In Orders _form i’ve put a ajax loaded content (Elements _form) to add a new element in Orders.

On saving the element its order_id is null (the Orders.id is not set yet because it’s in creation action), saving is done also in ajax.

Tried to save the Order on the displaying the element form, but is i do a page refresh it creates many records.

How to manage it?

Any ideas will be appreciated.

Hi EndErr,

So Order HAS_MANY Elements …

As you have noticed, you have to save Order first to get the id of it.

I would not save the elements to db when the user has added/removed elements via ajax. They could be retrieved as an array of Element objects only in the memory while they are getting passed back and forth between the server and the client via ajax.

This sounds like another problem.