首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hstore和Rails

Hstore和Rails
EN

Stack Overflow用户
提问于 2012-12-07 15:44:05
回答 1查看 3.1K关注 0票数 2

我试图在Rails 3.2.9项目中使用ActiveRecord Hstore的最新版本,但我在使用ActiveRecord为Hstore中的值提供的ActiveRecord时遇到了一些困难。

我的模型看起来是这样的:

代码语言:javascript
复制
class Person < ActiveRecord::Base
  serialize :data, ActiveRecord::Coders::Hstore
  attr_accessible :data, :name
  store_accessor :data, :age, :gender
end

当我进入rails控制台时,我得到的是:

代码语言:javascript
复制
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"> 

注意保存!更新这个人。当我退出控制台并返回记录时,返回到原始数据集。

另外,请注意,当我尝试访问age时,它返回零。

如果有人想知道store_accessor方法在ActiveRecord中做什么

store.rb

代码语言:javascript
复制
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        

这很奇怪,因为当我在控制台中执行相同的send(store_attribute)[key]行时,它可以工作:

代码语言:javascript
复制
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 > 

知道这里可能发生了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-07 16:24:24

最后,我将所有的store_accessor放在一起,并使用下面的代码为hstore键添加访问器

lib/hstore_accessor.rb

代码语言:javascript
复制
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)

config/initializers/active_record_extensions.rb

代码语言:javascript
复制
require "hstore_accessor"

Person

代码语言:javascript
复制
class Person < ActiveRecord::Base
  hstore_accessor :data, :size, :shape, :smell
end
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13766300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档