# Controlling content\_type in attachment\_fu

**URL:** <https://www.ruby-forum.com/t/controlling-content-type-in-attachment-fu/127500>\
**Category:** Rails\
**Created:** [January 29, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/controlling-content-type-in-attachment-fu/127500 "2008-01-29T21:28:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 29, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/controlling-content-type-in-attachment-fu/127500/1 "2008-01-29T21:28:03Z")

</div>

I’m working on an app where a model can have different kinds of audio  
attachments, in this case mp3 and m4a files. I want to distiguish  
between them by content type. So I want mp3 files to have a content  
type of audio/mpeg and m4a files a content type of audio/mp4, at first  
I tried this:

class AudioFile \< ActiveRecord::Base  
has\_attachment :storage =\> :file\_system,  
:max\_size =\> 10.megabytes,  
:processor =\> :none

def self.create\_mp3!(attributes)  
create!(attributes.merge(:content\_type =\> “audio/mpeg”))  
end

def self.create\_mp3!(attributes)  
create!(attributes.merge(:content\_type =\> “audio/mpeg”))  
end  
end

where the attributes coming from a form included an uploaded\_data value.

This DIDN’T work, the content type was ignored, I eventually tracked  
this down to attachment\_fu setting the content type from content\_type  
returned by the UploadedTempFile value of uploaded data which was  
returning nil.

So right now, I’ve got this:

module MP3File  
def content\_type  
‘audio/mpeg’  
end  
end

module AACFile  
def content\_type  
‘audio/mp4’  
end  
end

def self.create\_mp3!(attributes)  
attributes[:uploaded\_data].extend MP3File  
create!(attributes.merge(:content\_type =\> ‘audio/mpeg’))  
end

def self.create\_aac!(attributes)  
attributes[:uploaded\_data].extend AACFile  
create!(attributes.merge(:content\_type =\> ‘audio/mp4’))  
end

Which ‘duck punches’ the UploadedTempFile to return the right value.

But this feels to me somehow wrong. Am I missing something in  
attachment\_fu?

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
