title: Rails return JSON serialized attribute with_indifferent_access
body: <p>I previously had:</p>
  
  <pre><code>serialize :params, JSON
  </code></pre>
  
  <p>But this would return the JSON and convert hash key symbols to strings. I want to reference the hash using symbols, as is most common when working with hashes. I feed it symbols, Rails returns strings. To avoid this, I created my own getter/setter. The setter is simple enough (JSON encode), the getter is:</p>
  
  <pre><code>  def params
      read_attribute(:params) || JSON.parse(read_attribute(:params).to_json).with_indifferent_access
    end
  </code></pre>
  
  <p>I couldn't reference <code>params</code> directly because that would cause a loop, so I'm using <code>read_attribute</code>, and now my hash keys can be referenced with symbols or strings. However, this does not update the hash:</p>
  
  <pre><code>model.params.merge!(test: 'test')
  puts model.params # =&gt; returns default params without merge
  </code></pre>
  
  <p>Which makes me think the hash is being referenced by copy.</p>
  
  <p>My question is twofold. Can I extend active record JSON serialization to return indifferent access hash (or not convert symbols to strings), and still have hash work as above with merge? If not, what can I do to improve my getter so that <code>model.params.merge!</code> works? </p>
  
  <p>I was hoping for something along the lines of (which works):</p>
  
  <pre><code>  def params_merge!(hash)
      write_attribute(:params, read_attribute(:params).merge(hash))
    end
  
    # usage: model.params_merge!(test: 'test')
  </code></pre>
  
  <p>Better yet, just get Rails to return a hash with indifferent access or not convert my symbols into strings! Appreciate any help.</p>