Hstore和Rails [英] Hstore and Rails

查看:224
本文介绍了Hstore和Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Hstore使用ActiveRecord的 - Postgres的-hstore创业板的最新版本的Rails 3.2.9项目,我有一点用在Hstore值提供的ActiveRecord的store_accessor麻烦。

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.

我的模式是这样的:

class Person < ActiveRecord::Base
  serialize :data, ActiveRecord::Coders::Hstore
  attr_accessible :data, :name
  store_accessor :data, :age, :gender
end

当我进入轨道控制台这里是我得到的:

When I go into the rails console here's what I get:

Loading development environment (Rails 3.2.9)
1.9.3-p327 :001 > p = Person.find(3)
  Person Load (9.8ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
 => #<Person id: 3, name: "Leo", data: {"age"=>"34", "gender"=>"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:27:40"> 
1.9.3-p327 :002 > p.age
 => nil 
1.9.3-p327 :003 > p.age = "204"
 => "204" 
1.9.3-p327 :004 > p.save!
   (0.2ms)  BEGIN
   (0.6ms)  UPDATE "people" SET "data" = 'age=>34,gender=>male,age=>204', "updated_at" = '2012-12-07 15:28:33.097404' WHERE "people"."id" = 3
   (2.2ms)  COMMIT
 => true 
1.9.3-p327 :005 > p
 => #<Person id: 3, name: "Leo", data: {"age"=>"204", "gender"=>"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:28:33">
1.9.3-p327 :006 > exit
% rails c                 12-12-07 - 10:28:35
Loading development environment (Rails 3.2.9)
1.9.3-p327 :001 > p = Person.find(3)
  Person Load (6.9ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
 => #<Person id: 3, name: "Leo", data: {"age"=>"34", "gender"=>"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:28:33"> 

注意保存!不更新的人。当我退出控制台,回来在记录可以追溯到原始数据集。

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.

另外,注意到,当我尝试访问年龄返回零。

Also, notice that when I try accessing the age it returns nil.

如果有人想知道这里是什么 store_accessor ​​方法会在ActiveRecord的

If anyone was wondering here's what the store_accessor method does in ActiveRecord

store.rb

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        

这是p $ ptty的怪异监守$当我做同样的发(store_attribute)[关键] 在它的工作原理控制台线路:

It's pretty weird becuase when I do the that same send(store_attribute)[key] line in the console it works:

1.9.3-p327 :010 >   p.send(:data)['age']
 => "34" 

1.9.3-p327 :011 > p.send(:data)['age'] = "500"
 => "500" 
1.9.3-p327 :012 > p.send("data_will_change!")
 => {"age"=>"500", "gender"=>"male"} 
1.9.3-p327 :013 > p.save!
   (0.2ms)  BEGIN
   (0.7ms)  UPDATE "people" SET "data" = 'age=>500,gender=>male', "updated_at" = '2012-12-07 15:41:50.404719' WHERE "people"."id" = 3
   (1.8ms)  COMMIT
 => true 
1.9.3-p327 :014 > p
 => #<Person id: 3, name: "Leo", data: {"age"=>"500", "gender"=>"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:41:50"> 
1.9.3-p327 :015 > exit
% rails c                 12-12-07 - 10:41:57
Loading development environment (Rails 3.2.9)
1.9.3-p327 :001 > p = Person.find(3)
  Person Load (9.3ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
 => #<Person id: 3, name: "Leo", data: {"age"=>"500", "gender"=>"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:41:50"> 
1.9.3-p327 :002 > 

什么可能发生在这里的任何想法?

Any idea of what might be happening here?

推荐答案

我结束了滴速的store_accessor一起,用下面的code添加访问器hstore键

I ended up droping the store_accessor all together and using the following code to add accessors for hstore keys

的lib / hstore_accessor.rb

module HstoreAccessor
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def hstore_accessor(hstore_attribute, *keys)
      Array(keys).flatten.each do |key|
        define_method("#{key}=") do |value|
          send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value))
          send("#{hstore_attribute}_will_change!")
        end
        define_method(key) do
          send(hstore_attribute) && send(hstore_attribute)[key.to_s]
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, HstoreAccessor)

配置/初始化/ active_record_extensions.rb

require "hstore_accessor"

class Person < ActiveRecord::Base
  hstore_accessor :data, :size, :shape, :smell
end

这篇关于Hstore和Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆