Dynamic Attachment Size for Paperclip (Rails)

paperclip, ruby-on-rails, validation

Solution

Paperclip doesn't allow to pass function as size limit parameter. So you probably need to write custom validation:

  validate :validate_image_size

  def validate_image_size
    if document.file? && document.size > get_current_file_size_limit
      errors.add_to_base(" ... Your error message")
    end
  end

Problem

Is there anyway to have the validates_attachment_size except a dynamic file size limit? Here's an example: ``` class Document < ActiveRecord::Base belongs_to :folder has_attached_file :document validates_attachment_size :document, :less_than => get_current_file_size_limit private def get_current_file_size_limit 10.megabytes # This will dynamically change end end ``` I've tried this but I keep getting an error saying "unknown method". Lambdas and Procs don't work either. Has anyone ever tried this? Thanks

Original source