When working with file uploads, I came across some unexpected behaviour in the file_column plugin. If you submit a form containing an empty file input, a StringIO object will still be created for it.
The example upload is a confidentiality agreement. When you upload a file, params[:confidentiality_agreement][:file] is populated with the StringIO object for the file. Unfortunately, the same happens when the file upload field is empty!
raise params[:confidentiality_agreement][:file].inspect
… produces something like StringIO:0xb7ae3978.
So in order to check whether or not a new file has been uploaded, you need to check the size of the StringIO object as well as checking if the field is blank. For example:
# has a new confidentiality agreement been uploaded?
if params[:confidentiality_agreement][:file].blank? || params[:confidentiality_agreement][:file].size == 0
# no new file, use existing one
else
# new file has been uploaded, save it!
end
Sources:
StringIO docs