Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:05:10+00:00 2026-05-14T15:05:10+00:00

I think I have a bit of a chicken and egg problem. I would

  • 0

I think I have a bit of a chicken and egg problem. I would like to set the content_type of a file uploaded via Paperclip. The problem is that the default content_type is only based on extension, but I’d like to base it on another module.

I seem to be able to set the content_type with the before_post_process

class Upload < ActiveRecord::Base 
  has_attached_file :upload
  before_post_process :foo

  def foo
    logger.debug "Changing content_type"

    #This works
    self.upload.instance_write(:content_type,"foobar")

    # This fails because the file does not actually exist yet
    self.upload.instance_write(:content_type,file_type(self.upload.path)
  end

  # Returns the filetype based on file command (assume it works)
  def file_type(path)
    return `file -ib '#{path}'`.split(/;/)[0]
  end
end

But…I cannot base the content type on the file because Paperclip doesn’t write the file until after_create.

And I cannot seem to set the content_type after it has been saved or with the after_create callback (even back in the controller)

So I would like to know if I can somehow get access to the actual file object (assume there are no processors doing anything to the original file) before it is saved, so that I can run the file_type command on that. Or is there a way to modify the content_type after the objects have been created.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:05:11+00:00Added an answer on May 14, 2026 at 3:05 pm

    Probably you could use upload.to_file. It gives you paperclip temporary file (Paperclip::Tempfile). It has path property, so you can use

    self.upload.instance_write(:content_type,file_type(self.upload.to_file.path)
    

    You can get Tempfile using upload.to_file.to_tempfile

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have this right but I would like someone to verify that.
i saw that sometimes javascript timers have a bit of lag. I would like
I have a bit of script that I think is nearly there. I have
I have this bit of PHP that I'd like to do the equivalent of
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
I think I have a basic understanding of this, but am hoping that someone
I have an issue that (I think) might have to do with scope, but
I think I have a problem with casting. $db = mysqli_connect('127.0.0.1','root','password','test'); if (! $db)
I think I have a big problem. I have a two projects solution. First
I think I have a synchronization problem...It may be too basic..Please help.. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.