Uploading images for a model that isn't yet saved

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