# How to pass something to a model with has\_attachment?

**URL:** <https://www.ruby-forum.com/t/how-to-pass-something-to-a-model-with-has-attachment/151214>\
**Category:** Rails\
**Created:** [October 18, 2008, 8:01am UTC](https://www.ruby-forum.com/t/how-to-pass-something-to-a-model-with-has-attachment/151214 "2008-10-18T08:01:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![steffen\_w](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@steffen\_w](https://www.ruby-forum.com/u/steffen_w)\
**Post date:** [October 18, 2008, 8:01am UTC](https://www.ruby-forum.com/t/how-to-pass-something-to-a-model-with-has-attachment/151214/1 "2008-10-18T08:01:00Z")

</div>

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](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
