# Creating records with file upload in migration

**URL:** <https://www.ruby-forum.com/t/creating-records-with-file-upload-in-migration/158769>\
**Category:** Rails\
**Created:** [January 27, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/creating-records-with-file-upload-in-migration/158769 "2009-01-27T20:26:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 27, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/creating-records-with-file-upload-in-migration/158769/1 "2009-01-27T20:26:42Z")

</div>

Hi,  
I have a migration which creates several records along with the table.  
eg  
Forum.create :title =\> “General Discussion”, :description =\> “General  
Chit-Chat”

But I’d like to create a record which accepts an uploaded file, such as  
a jpeg. Is it possible to automate this using the above technique?

Thanks

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 27, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/creating-records-with-file-upload-in-migration/158769/2 "2009-01-27T20:38:34Z")

</div>

On Tue, Jan 27, 2009 at 2:26 PM, Dan S.  
\<removed\_email\_address@domain.invalid

> wrote:

> Thanks

How does your Forum model currently upload a file? Do you use an  
after\_save  
callback to write the file into your file storage directory? Could you  
modify your callback to accept a filename from somewhere else in the  
filesystem and have it simply copy that file to your file storage  
directory?

–wpd

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 28, 2009, 2:53am UTC](https://www.ruby-forum.com/t/creating-records-with-file-upload-in-migration/158769/3 "2009-01-28T02:53:02Z")

</div>

> I have a migration which creates several records along with the table.  
> eg  
> Forum.create :title =\> “General Discussion”, :description =\> “General  
> Chit-Chat”

I am not sure you want to combine migrations with record creation. It  
is probably best for you to handle this via a load script or a rake  
task.

As to the file upload, if you are using a plugin like attachment\_fu,  
then you are all set. Take a look at the plugin tests directory to see  
how those simulate the upload of files and create the uploaded files  
either in the db or in the path you specified in your models.
