How to pass something to a model with has_attachment?


#1

Hi,

being still quite new to Rails, Ruby and this whole cool programming
stuff (and not having done my homework in studiying the basics) this
problem is probably quite simple. But it still bugs me out as I have to
finish the site. Like last month.

I’ve used Advanced Rails Recipes
(http://media.pragprog.com/titles/fr_arr/upload_images_with_thumbnails.pdf)
to upload images for my products. Now the customer wants two more
categories - with other sizes for the attachment. I managed to
differentiate it when uploading the images in the first place. But
changing them doesn’t work. I simply don’t now how to tell
has_attachment which sizes are to be used.

Here’s what I have so far (I know, it’s ugly as hell, but it just has to
work. Good is saved for my second project.):

product_service.rb

def update_attributes(product_attributes, mugshot)
@product.attributes = product_attributes
unless mugshot.blank?
@mugshot.update_attributes(:uploaded_data => mugshot)
end
save
end

mugshot.rb

class Mugshot < ActiveRecord::Base

belongs_to :product

last_product = Product.find(:last)

if(last_product.category_id == 1)

   has_attachment  :content_type   => :image,
                   :storage        => :file_system,
                   :max_size       => 500.kilobytes,
                   :resize_to      => '308x364>',
                   :thumbnails     => {
                     :cart     =>  '55x65>',
                     :carousel =>  '144x170>'
                   }

 else

   has_attachment  :content_type   => :image,
                   :storage        => :file_system,
                   :max_size       => 500.kilobytes,
                   :resize_to      => '308x225>',
                   :thumbnails     => {
                     :cart     =>  '64x47>',
                     :carousel =>  '122x89>'
                   }

 end

 validates_as_attachment

end

I use last_product (which is probably pretty ugly) to determine, which
category the product has, that was just created. My question: How can I
pass a variable to mugshot.rb when updating my product.

Thanks in advance for suggestions. And yes, when I finish this project,
I will start with DRY, MVC, RESTfulness and all the things that make so
much sense in Rails.

Steffen