# Uploading images for a model that isn't yet saved

**URL:** <https://www.ruby-forum.com/t/uploading-images-for-a-model-that-isnt-yet-saved/112355>\
**Category:** Rails\
**Created:** [August 17, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/uploading-images-for-a-model-that-isnt-yet-saved/112355 "2007-08-17T20:45:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [August 17, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/uploading-images-for-a-model-that-isnt-yet-saved/112355/1 "2007-08-17T20:45:30Z")

</div>

In a couple of applications I’m working on I have a need to allow  
users to upload images which are then tied to something they’re  
creating by filling out a form (a model in my DB). In both cases I’d  
like them to be able to upload the images on the same page as the form  
in an ajax-y way. Obviously this leads me to a chicken-and-egg problem

- images must belong to a model and refer to its id, but the model has  
not yet been saved and has no id.

I’m looking for a strategy to remedy this. The only thing I’ve come up  
with is to allow users to upload images that initially aren’t tied to  
a model, and then add the model’s id to the image’s row in the DB when  
the model is saved. Later, a scheduled reaper comes along looking for  
images which have no associated model and which were created more than  
X amount of time in the past, and destroying them. I’m not crazy about  
the idea of a scheduled reaper (though I’m not entirely allergic to  
it), and somehow I’d have to store information (probably in the  
session) that ties the images to the not-yet-finished model until the  
connection can be made in the DB.

I’m really hoping there’s some more obvious (and hopefully simpler)  
solution I’m overlooking. Any ideas?

Thanks . . .

–  
Bill K.

[http://bkocik.net](http://bkocik.net)
