Kyle Decot
Kyle Decot

Reputation: 20815

Carrierwave dynamic extension_white_list

I have two models (Document & DocumentType). Using carrierwave I want to dynamically restrict the file extensions allowed on the Document based on it's DocumentType (which holds an array of acceptable file extensions). The problem is that extension_white_list seems to get called before the DocumentType gets associated with the Document. Ideas, Thoughts?

def create
  @document = Document.new document_params
end

Upvotes: 1

Views: 568

Answers (1)

apneadiving
apneadiving

Reputation: 115521

In your uploader, you can do whatever you need, class, instance methods or fixed data.

def extension_white_list
  # Document.some_class_method
  # model.some_instance_method
  # fixed: %w(jpg jpeg gif png)
end

The issue could stem from the way Rails assigns params: you can't control the order.

In this case, split the lines to get the order you desire:

@document = Document.new document_params_without_file
@document.assign_attributes document_file_params
#or a mere @document.file_accessor = document_file_params

Upvotes: 3

Related Questions