Search code examples
ruby-on-railspaperclippaperclip-validation

Paperclip override validates_attachment in subclass


I am trying to override validates_attachment in Subclass but I notice it only works well with Superclass validation; I wonder why my validates_attachment in subclass doesn't work. Has anybody faced this problem? and how have you solved this problem? Here is an example code:

class Superclass
    validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "28", "4:1" => "50", "5:1" => "40"} }
end

class Subclass < Superclass
  validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "40", "2:1" => "60"} }
end

Solution

  • I suggest that you should put both the class's fields in different tables. It could be possible that you are getting problems because of that.

    However if you really want to have only one table for both the classes then I believe that you could use something like this:

    validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "40", "2:1" => "60"} }, :unless => Proc.new {|attach| attach.type == "SubClass"}
    

    I assumed that you have a attach_type column but depending on how you are determining whether the attachment type is a SubClass, it is left upto you to change it.

    You could also try to remove your validates_attachment from the Subclass and instead try with_options in your model, like the following:

    with_options :unless => :attach_type == "SubClass" do |attach|
       attach.validates_attachment :logo, :image_ratio => { :ratio  => {"1:1" => "40", "2:1" => "60"}}
    end