Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby/Rails inheritance, overriding validators defined in super-class in sub-class
    primarykey
    data
    text
    <p>I have the following rails/paperclip validator:</p> <pre><code>class ImageRatioValidator &lt; ActiveModel::EachValidator def validate_each(record, attribute, value) attr_name = "#{attribute}_width".to_sym value = record.send(:read_attribute_for_validation, attribute) valid_ratios = options[:ratio] if !value.queued_for_write[:original].blank? geo = Paperclip::Geometry.from_file value.queued_for_write[:original] # first we need to validate the ratio cur_ratio = nil valid_ratios.each do |ratio, max_width| parts = ratio.split ":" next unless (geo.height * parts.first.to_f) == (geo.width * parts.last.to_f) cur_ratio = ratio end # then we need to make sure the maximum width of the ratio is honoured if cur_ratio if not valid_ratios[cur_ratio].to_f &gt;= geo.width record.errors[attribute] &lt;&lt; "The maximum width for ratio #{cur_ratio} is #{valid_ratios[cur_ratio]}. Given width was #{geo.width}!" end else record.errors[attribute] &lt;&lt; "Please make sure you upload a stall logo with a valid ratio (#{valid_ratios.keys.join(", ")})!" end end end end </code></pre> <p>the validator is used in both super (super-class is not abstract so can be instanciated) and sub-class. In the sub-class I need to change the ratios allowed:</p> <p>Super-class:</p> <pre><code>class Superclass validates_attachment :logo, :image_ratio =&gt; { :ratio =&gt; {"1:1" =&gt; "28", "4:1" =&gt; "50", "5:1" =&gt; "40"} } end </code></pre> <p>Sub-class:</p> <pre><code>class Subclass &lt; Superclass validates_attachment :logo, :image_ratio =&gt; { :ratio =&gt; {"1:1" =&gt; "40", "2:1" =&gt; "60"} } end </code></pre> <p>The validator works as expected in the super-class however seems to ignore the new ratios given in the sub-class.<br> Am I trying to use the validators in a non Rails way? How should I be using the validators in a situation like the one described above?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload