Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Paperclip is not compatible with STI #293

Closed
bogdan opened this issue Sep 13, 2010 · 11 comments
Closed

Paperclip is not compatible with STI #293

bogdan opened this issue Sep 13, 2010 · 11 comments
Assignees
Labels
Milestone

Comments

@bogdan
Copy link

bogdan commented Sep 13, 2010

Paperclip is completely uncompatible with Single Table inheritance.

We have the following architecture in our project:

class Blob < ActiveRecord::Base

  has_attached_file :file

  validates_attachment_presence :file

end

class Document < Blob

 validates_attachment_content_type :file, 
   :content_type => [
     'application/msword',
     'application/pdf',
     "text/plain",
     "text/html",
     "application/x-doc",
     "application/x-docx",
     "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
   ]

end


class Image < Blob

  validates_attachment_content_type :file,
    :content_type => [
    "image/jpeg",
    "image/gif",
    "image/png"
  ]

end

In this case papelclip populates all validation across all three models which is incorrect.
This behavior also will occur for multiple attachments definishions because they are writen using:
def attachment_definitions
read_inheritable_attribute(:attachment_definitions)
end

@ghost ghost assigned sikachu Jul 1, 2011
@atd
Copy link

atd commented Jul 6, 2011

It worked in paperclip 2.3.11 but it was broken in fd6eba3

@sikachu
Copy link
Contributor

sikachu commented Jul 6, 2011

Ha, let me check.

@a-chernykh
Copy link

It appears to be broken for me too.

@s-andringa
Copy link
Contributor

There's another issue with STI (or inheritance in general) when calling has_attached_file again on a subclass. Instead of adding an attachment definition to the subclass only, the attachment_definitions hash of the superclass is altered, and therefore it will apply to all subclasses. The superclass' attachment definitions hash is altered here: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip.rb#L266. This should be something like: self.attachment_definitions = attachment_definitions.merge(name => {:validations => []}.merge(options))

@ghost
Copy link

ghost commented Aug 17, 2011

Yep, I'm experiencing the same troubles as well.

@ermolaev
Copy link

ermolaev commented Sep 5, 2011

Yes, this is a real problem, only works with version 2.3.11

@ramn
Copy link
Contributor

ramn commented Sep 25, 2011

What do you think? #605

@sikachu
Copy link
Contributor

sikachu commented Oct 21, 2011

Fixed by #605. Please ping me if that's not the case and I will reopen this.

@sikachu sikachu closed this as completed Oct 21, 2011
@atd
Copy link

atd commented Nov 4, 2011

Waiting for this issue to test it:

jstorimer/delayed_paperclip#43

@atd
Copy link

atd commented Nov 14, 2011

Finally I could test it. It works perfectly. Please release! ;-)

@klebervirgilio
Copy link

That's issue is quite old.. Sorry for take it alive again.

Just would like to know if, when using STI plus this url config "/system/v/:hash.:extension"... The supercalss :hash will be different from the subclass :hash right?

For example:

class X < AR::Base
  has_attached_file :image,
        styles: { thumb: ["175x150", :png] },
        url: "/system/v/:hash.:extension",
        hash_secret: 'secret'
end

class Y < X
end

class Z < X
end

x = X.find(1) #=> <X  type=''>
y = Y.find(2) #=> <Y  type='Y'>
z = Z.find(3) #=> <Z  type='Z'>

x.image.url #=> '/public/system/v/::::hashX:::' # ok, that's expected

# :(, The image file was created with the X hash..  
# But when I try to retrive the url form y instance the url is generated with the Y hash.
y.image.url #=> '/public/system/v/::::hashY:::' 

z.image.url #=> '/public/system/v/::::hashZ:::' # :(

The image file, from Y and Z instances was created using the X hash.
But when I try to retrive the url form 'y' or 'z' instance the url generated with the 'Y', 'Z' hash respectively.

Any thoughts?

thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants