title: Rails merge temporary attributes to model object
body: <p>I have a Rails model object that has attributes maps to columns in a DB. I compute couple of temporary fields name and age which are not in the DB columns. Is there a way to merge them into the object temporarily without making changes to the model object?</p>
  <pre><code>class MyModel &lt; ApplicationRecord
  end
  </code></pre>
  <p>I tried using attributes.merge. and tried :</p>
  <pre><code>my_obj = my_obj.attributes.merge({ age: compute_age })
  
  my_obj = my_obj.attributes.merge({ name: compute_name })
  </code></pre>
  <p>The second merge however fails with NoMethodError: undefined method `attributes' for #Hash:0x0000000120060010.</p>
  <p>Looks like the moment I merge first one it makes the object a Hash. So I need to do</p>
  <pre><code>my_obj = my_obj.attributes.merge({ age: compute_age })
  
  my_obj[:name]=compute_name
  </code></pre>
  <p>All the other properties also are accessed as a hash.
  However this feels inconsistent and weird to me!</p>
  <p>Is there a better option to this than using attributes.merge i.e preserve the object?
  I guess it maybe cleaner to add the attr to the model object but those attributes are temporary and should not be accessed outside.</p>