title: View model attribute helper on attribute itself
body: <p>I have :meaning and :reading attributes on my view model. I want to run sanitize before_validation to clean user input before saving it to DB. Now, instead of typing something like this:</p>
  
  <pre><code>before_validation :sanitize_input
  
  def sanitize_input
    self.meaning = ActionController::Base.helpers.sanitize(self.meaning)
    self.reading = ActionController::Base.helpers.sanitize(self.reading)
  end
  </code></pre>
  
  <p>I wanted to make it a bit nicer. So I came up with an ActiveRecordExtension:</p>
  
  <pre><code>module ActiveRecordExtension
    extend ActiveSupport::Concern
  
    def sanitize_attribute(attribute)
      ActionController::Base.helpers.sanitize(attribute)
    end
  end
  
  ActiveRecord::Base.send(:include, ActiveRecordExtension)
  </code></pre>
  
  <p>Now I can call sanitize the input like this:</p>
  
  <pre><code>def sanitize_input
    self.meaning = sanitize_attribute(self.meaning)
    self.reading = sanitize_attribute(self.reading)
  end
  </code></pre>
  
  <p>I would like to shorten this a bit by doing something like this (kind of like helper method on the attribute itself) in my view model:</p>
  
  <pre><code>def sanitize_input
    self.meaning.sanitize_attribute!
    self.reading.sanitize_attribute!
  end
  </code></pre>
  
  <p>or</p>
  
  <pre><code>def sanitize_input
    sanitize_attribute!(self.meaning)
    sanitize_attribute!(self.reading)
  end
  </code></pre>
  
  <p>But whatever I tried, I could not make this work (various combinations of using replace and bang (!) in my sanitize_attribute method.</p>
  
  <p>Could it be shortened even further by using something like this:</p>
  
  <pre><code>def sanitize_attributes!(*args)
    args.each do |arg|
      arg.replace ActionController::Base.helpers.sanitize(arg)
    end
  end
  </code></pre>
  
  <p>and calling it with something like:</p>
  
  <pre><code>sanitize_attributes!(self.meaning, self.reading)
  </code></pre>
  
  <p>The last one would be handy for cases when there are multiple attributes that need to be sanitized. Can it somehow be done in one of the ways I want it to be done? </p>