# Attachment\_fu: loading objects w/ attachments in batch?

**URL:** <https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685>\
**Category:** Rails\
**Created:** [May 5, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685 "2008-05-05T15:17:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwhsix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@dwhsix](https://www.ruby-forum.com/u/dwhsix)\
**Post date:** [May 5, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685/1 "2008-05-05T15:17:20Z")

</div>

Using the (very nifty) attachment\_fu plugin. It would be very  
convenient if I could batch-load a lot of my model objects that have  
attachments, instead of having to load them interactively via a web  
page.

Experimented with calling uploaded\_data= directly, but didn’t work,  
and I’m not sure that’s the right route. Any suggestions?

Thanks,

dwh

---

<div class="post-metadata">

**Author:** ![dwhsix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@dwhsix](https://www.ruby-forum.com/u/dwhsix)\
**Post date:** [May 5, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685/2 "2008-05-05T16:44:11Z")

</div>

Not sure if this is what you’re after or not. We have some scenarios  
in which we create files (reports) on the webserver in response to a  
user request and then save them on the server using an attachment\_fu-  
based model. It’s similar in that we’re creating the model with a  
file but avoiding the interactive part.

It was as simple as setting .temp\_path. Note that this works  
for us because we’re storing objects in the file system rather than  
the DB/S3. temp\_path is mixed in for file system storage; not sure  
about the others.

---

<div class="post-metadata">

**Author:** ![dwhsix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@dwhsix](https://www.ruby-forum.com/u/dwhsix)\
**Post date:** [May 5, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685/3 "2008-05-05T17:06:41Z")

</div>

Hmm, that might work. Will investigate.

Thanks,

dwh

---

<div class="post-metadata">

**Author:** ![dwhsix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@dwhsix](https://www.ruby-forum.com/u/dwhsix)\
**Post date:** [May 11, 2008, 3:50am UTC](https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685/4 "2008-05-11T03:50:30Z")

</div>

Aha. [http://blog.lachaise.org/?p=8](http://blog.lachaise.org/?p=8) seems to provide a way to do this  
(in the middle of a discourse about duck typing…)

dwh

---

<div class="post-metadata">

**Author:** ![dwhsix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@dwhsix](https://www.ruby-forum.com/u/dwhsix)\
**Post date:** [May 11, 2008, 4:10am UTC](https://www.ruby-forum.com/t/attachment-fu-loading-objects-w-attachments-in-batch/136685/5 "2008-05-11T04:10:23Z")

</div>

Actually, having followed a suggestion in the comments to that blog  
entry, there’s even an easier way (below, ImageMetadata is my class  
that has\_attachment):

script/console

> > Mime::Type.register “image/gif”, :gif  
> > =\> [“gif”]
> 
> > t = ActionController::TestUploadedFile.new(‘db/seed-data//tile-samples/bippy2.gif’, Mime::GIF)  
> > =\> #\<ActionController::TestUploadedFile:0xb70358d8  
> > @original\_filename=“bippy2.gif”, @content\_type=#\<Mime::Type:0xb7039974  
> > @synonyms=[], @symbol=:gif, @string=“image/gif”\>, @tempfile=#\<File:/  
> > tmp/bippy214728-4.gif\>\>
> 
> > m = ImageMetadata.new  
> > =\> #\<ImageMetadata id: nil, size: nil, content\_type: nil, filename:  
> > nil, height: nil, width: nil, parent\_id: nil, thumbnail: nil,  
> > property\_value\_id: nil, created\_at: nil, updated\_at: nil\>
> 
> > m.uploaded\_data = t  
> > =\> #\<ActionController::TestUploadedFile:0xb70358d8  
> > @original\_filename=“bippy2.gif”, @content\_type=#\<Mime::Type:0xb7039974  
> > @synonyms=[], @symbol=:gif, @string=“image/gif”\>, @tempfile=#\<File:/  
> > tmp/bippy214728-4.gif\>\>
> 
> > m.save!  
> > =\> true

Seems like sorta a pain that you have to explicitly set the mime type,  
but hey, small price to pay…

dwh
