#9 √ resolved
Mike Lapshin

Paperclip doesn't work for derived classes

Reported by Mike Lapshin | April 14th, 2008 @ 05:35 AM

If attachment declared in base classes, exception is thrown when attachment is accessed in derived classes:

class User < ActiveRecord::Base
  has_attached_file :avatar,
                    :styles => { :small => "30x30", :big => "100x100" }
end

class RegularUser < User
end

class OpenIdUser < User
end

>> u = OpenIdUser.new
>> u.avatar
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
        from /home/sotakone/.../vendor/plugins/paperclip/lib/paperclip.rb:174:in `attachment_for'
        from /home/sotakone/.../vendor/plugins/paperclip/lib/paperclip.rb:124:in `avatar'
        from (irb):2

I did some research and figured out that problem lies in @attachment_definitions class variable. To work with derived classes, it should be implemented via read/write_inheritable_attribute.

Comments and changes to this ticket

  • Jon Yurek

    Jon Yurek April 15th, 2008 @ 05:26 PM

    • → State changed from “new” to “open”
    • → Assigned user changed from “” to “Jon Yurek”

    D'oh! I'll have that fixed soon.

  • Jon Yurek

    Jon Yurek April 16th, 2008 @ 10:42 AM

    • → State changed from “open” to “resolved”

    This is fixed in the latest version.

  • Akshay Surve

    Akshay Surve November 10th, 2008 @ 05:15 AM

    • → Tag changed from “” to “bug inheritance sti”

    I am still getting a similar error. So:

    
    >> u = OpenIdUser.new and
    >> u.avatar
    NoMethodError: You have a nil object when you didn't expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occurred while evaluating nil.[]
            from /Users/ak47surve/Documents/projects/ssync/vendor/plugins/paperclip/lib/paperclip.rb:246:in `attachment_for'
            from /Users/ak47surve/Documents/projects/ssync/vendor/plugins/paperclip/lib/paperclip.rb:154:in `avatar'
            from (irb):2
    

    While .avatar on User.new works as intended.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket