# Using attachment fu in masse

**URL:** <https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202>\
**Category:** Rails\
**Created:** [May 31, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202 "2009-05-31T18:37:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e36b37/32.png) [@holden](https://www.ruby-forum.com/u/holden)\
**Post date:** [May 31, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202/1 "2009-05-31T18:37:07Z")

</div>

I’m trying to use attachment fu to add a large number of images at  
once using a script but i can’t even get it to add an image from the  
console… the other documentation i’ve found no longer seems to work.

Something like this doesn’t work properly

c = Image.new  
c.content\_type = ‘image/jpeg’  
c.filename = ‘test.jpg’  
c.discussion\_id = 7330  
c.uploaded\_data = File.open(’/Users/holden/Desktop/pictures/  
LionsFW.jpg’)  
c.save

It does save… but the file doesn’t get populated.

All the previous documentatio circa 2007 says :temp\_file but this is  
no longer available to i tried switching it to :uploaded\_data, but  
that doesn’t seem to work either…

also it want’s the size and is a required field, but shouldn’t this be  
automatically deduced… providing it doesn’t help either…

Any suggestions would be a great help,

Thanks,

holden

---

<div class="post-metadata">

**Author:** ![holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e36b37/32.png) [@holden](https://www.ruby-forum.com/u/holden)\
**Post date:** [May 31, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202/2 "2009-05-31T22:53:02Z")

</div>

Something like this almost work…

Discussion.new  
(:name=\>“test2”, :location\_id=\>“1036”, :image\_attributes=\>  
[{ :uploaded\_data=\> File.new("/Users/holden/Desktop/pictures/  
LionsFW.jpg") }], :website=\>“testdsfdf”, :geo=\>"", :phone=\>“test”,  
:blurb=\>“testdsfsdfsdf”, :address=\>“test”, :hwID=\>“test”,  
:email=\>“removed\_email\_address@domain.invalid”)

undefined method `[]’ for #\<File:/Users/holden/Desktop/pictures/  
LionsFW.jpg

---

<div class="post-metadata">

**Author:** ![holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e36b37/32.png) [@holden](https://www.ruby-forum.com/u/holden)\
**Post date:** [May 31, 2009, 11:23pm UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202/3 "2009-05-31T23:23:28Z")

</div>

Here’s how I’m doing it:

```
    file = ActionController::UploadedStringIO.new( data ) #this is

```

the real data  
file.instance\_variable\_set( ‘@original\_filename’,  
“#{rand(1000)}\_#{filename}” )  
file.content\_type = i.content\_type

```
    i.uploaded_data=( file )
    i.save!

```

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e36b37/32.png) [@holden](https://www.ruby-forum.com/u/holden)\
**Post date:** [June 1, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202/4 "2009-06-01T14:47:21Z")

</div>

You can try with the example in the attachment\_fu home (http://  
[GitHub - technoweenie/attachment\_fu: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.](http://github.com/technoweenie/attachment_fu/tree/master)):

i.uploaded\_data = ActionController::TestUploadedFile.new(file\_path,  
mimetype)

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e36b37/32.png) [@holden](https://www.ruby-forum.com/u/holden)\
**Post date:** [June 1, 2009, 11:24am UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202/5 "2009-06-01T11:24:23Z")

</div>

I tried using ActionController::UploadedStringIO.new( data )

and it does create the files… but the images don’t work.

Is data supposed to be the filepath? ie. photos/joe.jpg ?

I also found an example of exactly what I want to do in the Practical  
Rails Plugins book, but which is outdated using “temp\_data =” It  
looks simple and perfect, what am I missing?

> **[Practical Rails Plugins](https://books.google.com/books?id=c1L4IzmUzicC&pg=PA49&lpg=PA49&dq=attachment%2Bfu%2Bconsole%2Bcreation&source=bl&ots=0LhR2tApt6&sig=Kufto1uWSuhThKpy-dzVCzybKOI&hl=en&ei=Ca4iSoHZBMyOsAa36NC3Bg&sa=X&oi=book_result&ct=result&resnum=1)**
>
> The Rails framework empowers developers with unprecedented levels of productivity by embracing principles such as “convention over configuration” and “don’t repeat yourself”. This paradigm is even apparent at the community level, where developers...

On May 31, 11:22 pm, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![holden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e36b37/32.png) [@holden](https://www.ruby-forum.com/u/holden)\
**Post date:** [June 1, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/using-attachment-fu-in-masse/169202/6 "2009-06-01T15:02:35Z")

</div>

nevermind found this lurking in the documentation… i suppose i  
should have read it better before looking else where… ;-(

#!/usr/bin/env ./script/runner

# required to use ActionController::TestUploadedFile

require ‘action\_controller’  
require ‘action\_controller/test\_process.rb’

path = “./public/images/x.jpg”

# mimetype is a string like “image/jpeg”. One way to get the mimetype

for a given file on a UNIX system

# mimetype = `file -ib #{path}`.gsub(/\n/,"")

mimetype = “image/jpeg”

# This will “upload” the file at path and create the new model.

@attachable = AttachmentMetadataModel.new(:uploaded\_data =\>  
ActionController::TestUploadedFile.new(path, mimetype))  
@attachable.save
