# Acts\_as\_attachment - on server copy?

**URL:** <https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771>\
**Category:** Rails\
**Created:** [August 23, 2007, 3:53am UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771 "2007-08-23T03:53:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 23, 2007, 3:53am UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/1 "2007-08-23T03:53:39Z")

</div>

Hi all,

I am using aaa for uploads (Upload model) of assets to a dir below  
public. I also need to save files, into the Upload model, which come  
as attachments by email. Is there a way I can ask aaa to save the  
attachment as if it was uploaded? TIA,

bakki

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 23, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/2 "2007-08-23T15:54:44Z")

</div>

On 8/23/07, Bakki K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I am using aaa for uploads (Upload model) of assets to a dir below  
> public. I also need to save files, into the Upload model, which come  
> as attachments by email. Is there a way I can ask aaa to save the  
> attachment as if it was uploaded? TIA,
> 
> bakki

bakki,

you should be loop through the email’s attachments and create new  
Uploads like this:

for attachment in email.attachments  
Upload.create! attachment  
end

# note: this is untested code, but should point you in the right

direction.

Have a look at this article regarding Action Mailer’s attachments:

[http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer](http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer)

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 23, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/3 "2007-08-23T17:29:59Z")

</div>

Thank you for your reply Sam, I don’t have my action mailer receive  
part working yet and I will try this when its done. However the  
Upload model has

acts\_as\_attachment :storage =\> :file\_system, :file\_system\_path =\>  
‘uploads’  
validates\_as\_attachment

I was wondering if acts\_as\_attachment can take an attachment passed to  
the model vs. the uploaded file which it normally gets from cgi param.  
I thought the validates\_as\_attachment call back might not work if the  
attachment was passed to the model as you suggest. I’ll have to look  
the aaa code over to see if this doesn’t work.

-bakki

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 23, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/4 "2007-08-23T20:06:30Z")

</div>

here’s a post on how to upload files stored locally:

[http://www.railsweenie.com/forums/2/topics/1013](http://www.railsweenie.com/forums/2/topics/1013)

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 24, 2007, 11:05am UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/5 "2007-08-24T11:05:14Z")

</div>

Hello,

I am not sure if I understand correctly, but you can pass a regular  
file (on disk) to the model conviently using TestUploadedFile (see  
[http://api.rubyonrails.org/classes/ActionController/TestUploadedFile.html#M000278](http://api.rubyonrails.org/classes/ActionController/TestUploadedFile.html#M000278))  
like this:

Photo.create( :uploaded\_data =\>  
ActionController::TestUploadedFile.new(file, ‘image/jpeg’) )

See [http://pastie.caboo.se/90597](http://pastie.caboo.se/90597) for more complete code I use for  
importing whole directory of photos into application.

(You can even use this in script/console or script/runner, thus  
automating the importing process.)

Cheers,

Karel

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 24, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/6 "2007-08-24T19:41:38Z")

</div>

Hello,

yes, that’s it precisely. I guess acts\_as\_attachment has  
`TestUploadedFile` somehow encapsulated, but I couldn’t get it to  
work, don’t recall precisely why. I recently used `TestUploadedFile`  
to import loads of legacy data into ported application with _great_  
results.

Glad to help, cheers,

Karel

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [August 24, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-on-server-copy/112771/7 "2007-08-24T17:39:20Z")

</div>

Hi Karel,

This actually should work for me. I was looking at the code in  
acts\_as\_attachment and it has the following code which handles  
uploaded files.

```
  # This method handles the uploaded file object. If you set the

```

field name to uploaded\_data, you don’t need  
# any special code in your controller.  
#  
# \<% form\_for :attachment, :html =\> { :multipart =\> true } do  
|f| -%\>  
#

\<%= f.file\_field :uploaded\_data %\>

  
# 

\<%= submit\_tag :Save %\>  
# \<% end -%\>  
#  
# @attachment = Attachment.create! params[:attachment]  
def uploaded\_data=(file\_data)  
return nil if file\_data.nil? || file\_data.size == 0  
self.content\_type = file\_data.content\_type.strip  
self.filename = file\_data.original\_filename.strip  
self.attachment\_data = file\_data.read  
end

So this should fire when, as you suggest

Photo.create( :uploaded\_data =\>  
ActionController::TestUploadedFile.new(file, ‘image/jpeg’) )

:uploaded\_data gets assigned to and the rest of aaa validations etc  
should work.

Thank you for your help.

-bakki
