0

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?

Oliver Schobel
  • 377
  • 4
  • 20
  • 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 Sep 10 '15 at 13:09
  • 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 Sep 10 '15 at 13:41
  • Ok, so that is a new question perhaps :) Not sure on that one. – madcow Sep 10 '15 at 13:43
  • 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 Sep 10 '15 at 14:03
  • 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: http://stackoverflow.com/questions/2850418/rails-sti-prevent-base-class-from-instantiation – madcow Sep 10 '15 at 14:09

1 Answers1

0

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

madcow
  • 2,567
  • 14
  • 31