title: Mutating list of instantiated objects in Rails
body: <p>I'm attempting to clean up my view by moving Rails' sanitizer method to a helper, but it's not producing the desired result. So below is what my index action looks like. I know it's ugly and not very OOP, but I simplified it down so I could follow what was happening when debugging. </p>
  
  <p>I'm attempting to loop through all the sources' attributes, running the sanitizer on any attribute that is a non-empty string, replacing original strings with the sanitized strings (transform_values!), and writing over the original @sources (map!). </p>
  
  <p>I tried storing them in different variables than <code>@sources</code> and using <code>.each</code> instead of <code>.map!</code> but the sanitized values don't make it through. </p>
  
  <pre><code>def index
    @sources = Source.all
    @sources.map! { |source| 
      source.attributes.transform_values! { |attr|
        attr.blank? || !attr.is_a?(String) ? attr 
        : ActionController::Base.helpers.sanitize(attr) } }
  end
  </code></pre>
  
  <p>However, after examining my list of sources in the view, it's removing the source instances and instead returning a nondescript array of hashes. I can loop through these, but I can't call specific attributes like <code>source.author</code> which is not great.</p>
  
  <p>Here's some images for reference. The first one is what it should look like and second is what I'm currently getting</p>
  
  <p><a href="https://i.stack.imgur.com/zTALV.png" rel="nofollow noreferrer">Unsanitized sources</a></p>
  
  <p><a href="https://i.stack.imgur.com/EcCAm.png" rel="nofollow noreferrer">Sanitized sources</a></p>