Forum: Ruby on Rails problem with file_column storing images and other files

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.
Nithin R. (Guest)
on 2006-03-17 00:53
(Received via mailing list)
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?
This topic is locked and can not be replied to.