title: Hstore and Rails
body: <p>I'm trying to use Hstore in a Rails 3.2.9 project using the latest version of the activerecord-postgres-hstore gem and I am having a bit of trouble using the store_accessor provided by ActiveRecord for the values in the Hstore.</p>
  
  <p>My model looks like this: </p>
  
  <pre><code>class Person &lt; ActiveRecord::Base
    serialize :data, ActiveRecord::Coders::Hstore
    attr_accessible :data, :name
    store_accessor :data, :age, :gender
  end
  </code></pre>
  
  <p>When I go into the rails console here's what I get: </p>
  
  <pre><code>Loading development environment (Rails 3.2.9)
  1.9.3-p327 :001 &gt; p = Person.find(3)
    Person Load (9.8ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
   =&gt; #&lt;Person id: 3, name: "Leo", data: {"age"=&gt;"34", "gender"=&gt;"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:27:40"&gt; 
  1.9.3-p327 :002 &gt; p.age
   =&gt; nil 
  1.9.3-p327 :003 &gt; p.age = "204"
   =&gt; "204" 
  1.9.3-p327 :004 &gt; p.save!
     (0.2ms)  BEGIN
     (0.6ms)  UPDATE "people" SET "data" = 'age=&gt;34,gender=&gt;male,age=&gt;204', "updated_at" = '2012-12-07 15:28:33.097404' WHERE "people"."id" = 3
     (2.2ms)  COMMIT
   =&gt; true 
  1.9.3-p327 :005 &gt; p
   =&gt; #&lt;Person id: 3, name: "Leo", data: {"age"=&gt;"204", "gender"=&gt;"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:28:33"&gt;
  1.9.3-p327 :006 &gt; exit
  % rails c                 12-12-07 - 10:28:35
  Loading development environment (Rails 3.2.9)
  1.9.3-p327 :001 &gt; p = Person.find(3)
    Person Load (6.9ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
   =&gt; #&lt;Person id: 3, name: "Leo", data: {"age"=&gt;"34", "gender"=&gt;"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:28:33"&gt; 
  </code></pre>
  
  <p>Notice that the save! does update the person. When I exit the console and come back in the record goes back to the original set of data.</p>
  
  <p>Also, notice that when I try accessing the <code>age</code> it returns nil.</p>
  
  <p>If anyone was wondering here's what the <code>store_accessor</code> method does in ActiveRecord</p>
  
  <p><strong>store.rb</strong></p>
  
  <pre><code>def store_accessor(store_attribute, *keys)
    Array(keys).flatten.each do |key|
      define_method("#{key}=") do |value|
        send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
        send(store_attribute)[key] = value
        send("#{store_attribute}_will_change!") 
      end
  
      define_method(key) do
        send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
        send(store_attribute)[key]
      end
    end
  end        
  </code></pre>
  
  <p>It's pretty weird becuase when I do the that same <code>send(store_attribute)[key]</code> line in the console it works:</p>
  
  <pre><code>1.9.3-p327 :010 &gt;   p.send(:data)['age']
   =&gt; "34" 
  
  1.9.3-p327 :011 &gt; p.send(:data)['age'] = "500"
   =&gt; "500" 
  1.9.3-p327 :012 &gt; p.send("data_will_change!")
   =&gt; {"age"=&gt;"500", "gender"=&gt;"male"} 
  1.9.3-p327 :013 &gt; p.save!
     (0.2ms)  BEGIN
     (0.7ms)  UPDATE "people" SET "data" = 'age=&gt;500,gender=&gt;male', "updated_at" = '2012-12-07 15:41:50.404719' WHERE "people"."id" = 3
     (1.8ms)  COMMIT
   =&gt; true 
  1.9.3-p327 :014 &gt; p
   =&gt; #&lt;Person id: 3, name: "Leo", data: {"age"=&gt;"500", "gender"=&gt;"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:41:50"&gt; 
  1.9.3-p327 :015 &gt; exit
  % rails c                 12-12-07 - 10:41:57
  Loading development environment (Rails 3.2.9)
  1.9.3-p327 :001 &gt; p = Person.find(3)
    Person Load (9.3ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
   =&gt; #&lt;Person id: 3, name: "Leo", data: {"age"=&gt;"500", "gender"=&gt;"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:41:50"&gt; 
  1.9.3-p327 :002 &gt; 
  </code></pre>
  
  <p>Any idea of what might be happening here?</p>