Nithin Reddy
2006-Mar-16 22:52 UTC
[Rails] problem with file_column storing images and other files
I have a model called ProductFiles which associates an arbitrary amount of image and documents with a product. I am using the file_column plugin to manage the upload and storage of the images, like so: class ProductFile < ActiveRecord::Base # Accepted MIME types, mime types that are not part of this list are rejected MIME_EXTENSIONS = { "image/gif" => "gif", "image/jpg" => "jpg", "image/png" => "png", "application/pdf" => "pdf", "application/msword" => "doc", "application/zip" => "zip", "text/plain" => "txt", "text/xml" => "xml", "video/mpeg" => "mpeg", "video/quicktime" => "mov", } belongs_to :product validates_presence_of :file_name, :product_id file_column( :file_name, :options => {:mime_extensions => MIME_EXTENSIONS}, :magick => {:versions => {:small => {:crop => ''1:1'', :size => "75x75"}, :medium => {:size => "150x150"} } } ) end My problem is that when I include the :magick options hash to deal with images, file_column doesn''t accept other types of files anymore: only images. I figured a way around this is to do something like: class ProductFile < ActiveRecord::Base # Accepted MIME types, mime types that are not part of this list are rejected IMAGE_EXTENSIONS = { "image/gif" => "gif", "image/jpg" => "jpg", "image/png" => "png" } OTHER_EXTENSIONS = { "application/pdf" => "pdf", "application/msword" => "doc", "application/zip" => "zip", "text/plain" => "txt", "text/xml" => "xml", "video/mpeg" => "mpeg", "video/quicktime" => "mov", } belongs_to :product validates_presence_of :file_name, :product_id file_column( :image_name, :options => {:mime_extensions => IMAGE_EXTENSIONS}, :magick => {:versions => {:small => {:crop => ''1:1'', :size => "75x75"}, :medium => {:size => "150x150"} } } ) file_column( :file_name, :options => {:mime_extensions => OTHER_EXTENSIONS}) end This seems a bit messy, as either one or the other column will be filled out at any given time. Is there a better solution?