# Attachment\_fu - uploading from different models in the same action

**URL:** <https://www.ruby-forum.com/t/attachment-fu-uploading-from-different-models-in-the-same-action/154056>\
**Category:** Rails\
**Created:** [November 24, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/attachment-fu-uploading-from-different-models-in-the-same-action/154056 "2008-11-24T23:22:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shagymoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@shagymoe](https://www.ruby-forum.com/u/shagymoe)\
**Post date:** [November 24, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/attachment-fu-uploading-from-different-models-in-the-same-action/154056/1 "2008-11-24T23:22:05Z")

</div>

Hi,

I’ve got a situation where I want to upload a zip file and an image  
from the same form. Each attachment is a different model. So, here’s  
what I’ve got.

Models:

class LogoImage \< ActiveRecord::Base  
belongs\_to :company

has\_attachment :max\_size =\> 1.megabytes,  
:storage =\> :s3,  
:s3\_config\_path =\> (RAILS\_ROOT + ‘/config/  
companies\_s3.yml’),  
:content\_type =\> :image,  
:thumbnails =\> { :large =\> ‘96x96\>’ ,  
:medium =\> ‘64x64\>’ ,  
:small =\> ‘48x48\>’},  
:processor =\> :MiniMagick

validates\_as\_attachment  
end

class ZipFile \< ActiveRecord::Base  
belongs\_to :apps

has\_attachment :max\_size =\> 1.megabytes,  
:storage =\> :s3,  
:s3\_config\_path =\> (RAILS\_ROOT + ‘/config/  
apps\_s3.yml’),  
:content\_type =\> :image,  
:thumbnails =\> { :large =\> ‘96x96\>’ ,  
:medium =\> ‘64x64\>’ ,  
:small =\> ‘48x48\>’},  
:processor =\> :MiniMagick

validates\_as\_attachment  
end

class App \< ActiveRecord::Base  
has\_one :zip\_file  
belongs\_to :companies  
end

class Company \< ActiveRecord::Base  
has\_one :logo\_image  
has\_one :app  
end

#### View

\<%= error\_messages\_for :company %\>  
\<%= error\_messages\_for :app %\>  
\<%= error\_messages\_for :zip\_file %\>  
\<%= error\_messages\_for :logo\_image %\>

\<% form\_tag({:action =\> “new\_app”}, { :multipart=\>true }) do -%\>  
\<%= file\_field\_tag “zip\_file” %\>  
\<%= file\_field\_tag “logo\_image” %\>  
\<% end %\>

### CONTROLLER

def new\_app  
@app = params[:id].nil? ? App.new : App.find(params[:id])  
@company = Company.new(params[:company])  
@zip\_file = ZipFile.new(:uploaded\_data =\> params[:zip\_file])  
@logo\_image = LogoImage.new(:uploaded\_data =\> params[:logo\_image])

if request.post?  
@app.attributes = params[:app]  
@company.attributes = params[:company]

```
App.transaction do
  @company.save!

  @logo_image.company_id = @company.id
  @logo_image.save!

  @app.company_id = @company.id
  @app.save!

  @zip_file.app_id = @app.id
  @zip_file.save!
end

```

end  
end

## config files

apps\_s3.yml

development:  
bucket\_name: apps\_development  
access\_key\_id: id  
secret\_access\_key: key

companies\_3.yml

development:  
bucket\_name: companies\_development  
access\_key\_id: id  
secret\_access\_key: key

################3

Of course, there is a lot more info in there, but I tried to condense  
it. The issue is that everything is getting saved, but the logo image  
is getting saved in the same bucket as the zip file. It seems that  
attachment\_fu is just taking the bucket from the last attachment saved  
and putting them both there. Either this is a bug or I’m doing  
something wrong or it isn’t meant to do what I’m trying to do.

Any ideas?

Thanks.
