0
votes

I'm using Paperclip in my rails app to upload images to the filesystem. I furthermore use single table inheritance for an Incident model. Damage inherits from Incident. When I now create a new Incident object of :type => 'Damage' with a photo attached, something strange happens:

  • object.class --> Damage
  • object.url --> "/system/damages/photo_images/000/000/265/original/my_image.png?1441880763"
  • object.path --> "/Users/fuzz/keeja/backend/keeja_backend/public/system/damages/photo_images/000/000/265/original/fav_icon.png"

    has_attached_file :photo_image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

And now comes the strange part. The actual path of the uploaded file is not one of the above, but: /Users/fuzz/keeja/backend/keeja_backend/public/system/incidents/photo_images/000/000/265/original/fav_icon.png"

So Paperclip stores the file under a different path on the file system, than object.path says.

Can anyone please help me find out what's going wrong here?

1
Out of curiosity -- When you initialize your class, do you specify Damages.new / Damages.create / etc or Incidents.new / Incidents.create / etc? It sounds like you may be working with the abstract class, not a subclass. Try directly creating an instance of the subclass if not.madcow
I do a Incident.new in the create method of the IncidentsController. When I use Damage.new, it works. Even though it doesn't create the :medium and :thumb version of the image (see the edit).Oliver Schobel
Ok, so that is a new question perhaps :) Not sure on that one.madcow
Yeah, you're right. Thanks for the hint. I don't know why Paperclip is behaving this way, but at least it's working now :-)Oliver Schobel
It's because in STI the base class can be instantiated so Paperclip thinks it's a different model. Check this out if you want to prevent such bugs, it may be helpful: stackoverflow.com/questions/2850418/…madcow

1 Answers

0
votes

Use Damage.new / Damage.create / etc. instead of instantiating a new Incident to solve the path problem.