Forum: Ruby on Rails attachment_fu - uploading from different models in the same action

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4bb313af528d968c3a35e8208ecd287b?d=identicon&s=25 Shagy Moe (shagymoe)
on 2008-11-24 23:22
(Received via mailing list)
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.
This topic is locked and can not be replied to.