2014-12-04 45 views
1

我的模型中有一個Hash屬性,使用Postgres hstore擴展。問題是這個屬性被Rails4轉換爲String。這可以防止我進行基本操作,例如.each.map來處理我的哈希屬性。Rails4:我的hstore屬性被轉換爲字符串

使用Rails控制檯,不會轉換哈希。打字:

@device.data 
@device.data.class 

給出了Rails的控制檯:

{"city"=>"London", "owner_name"=>"John"} 
Hash 

和應用程序本身(使用導航器):

"\"city\"=>\"London\","\"owner_name\"=>\"John\" 
String 

你有什麼想法?

更新:

這裏是模型:

class Device < ActiveRecord::Base 

    belongs_to :company 
    has_many :records 

    validates :name, presence: true 
end 

和相應的遷移文件:

class CreateDevices < ActiveRecord::Migration 
    def change 
    create_table :devices do |t| 

     t.string :name 
     t.hstore :data 
     t.integer :company_id 

     t.timestamps 
    end 

    add_index :devices, :name 
    end 
end 
+1

Postgres'hstore'讓你的哈希像一個字符串,這是正常的行爲。當列加載'hstore'時,Rails會[[序列化]](http://en.wikipedia.org/wiki/Serialization) – 2014-12-04 07:59:22

+0

您可以發佈您的模型代碼。 – DickieBoy 2014-12-04 08:36:24

+0

@DickieBoy我添加了模型和遷移文件。 @Зелёный但是,在這種情況下,我怎樣才能使用散列,就像'@ device.data ['country']'? – htaidirt 2014-12-04 09:14:13

回答

-1

嘗試刪除您的TMP文件夾並重新啓動所有服務器。

rm -rf tmp/* 
相關問題