small medium large xlarge

Generic-user-small
28 Oct 2008, 11:16
Steffen Wenzel (2 posts)

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 in… last month.

I 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

Generic-user-small
29 Oct 2008, 10:28
Steffen Wenzel (2 posts)

I know most of you will probably pull their hairs out now, but at least I found a solution:

The product_service.rb writes a temporary value into a specific database-table, which mugshot.rb reads later. Hell, this is ugly. I’ll probably be laughing at myself in six months.

Steffen

You must be logged in to comment