2013-04-03 34 views
1

不somoone知道是否有可能在軌道4,5 hstore的陣列?我tryied與hstore的Rails的PostgreSQL的陣列

add_column :orders, :frozen_content, :hstore , array: true 

,但我得到

PG::Error: ERROR: malformed array literal: 

當我嘗試保存

回答

0

你可以起訴的ActiveRecord-的Postgres-hstore寶石:

https://github.com/engageis/activerecord-postgres-hstore

從文檔:

  1. 創建hstore支持的領域:

    類Person <的ActiveRecord :: Base的 連載:數據的ActiveRecord ::打碼機:: Hstore 結束

  2. 字段添加到它:

    人= Person.new person.data [ '富'] = '酒吧' person.save

  3. 查詢它:

    Perosn.where( 「數據 - > '富'= '巴'」)

Railscast#345(這是一個付費牆後面)覆蓋更詳細使用hstore,使用ActiveRecord的 - Postgres的-hstore寶石:

http://railscasts.com/episodes/345-hstore

注:我還沒有與軌道4試了一下...因人而異。

+1

我試圖在這裏做HSTORE陣列。隨着經典的Hstore列我沒有問題 – Piotr

3

原則,肯定的,但你發現它不被正確保存時逃跑。我剛今天登錄有關的問題,請參見https://github.com/rails/rails/issues/11135(包括修復補丁和一些演示代碼)

+0

我不知道這是否是相同的問題。至少,我設法用這個其他小片來修復這個bug:https://github.com/rails/rails/pull/11477 – Ashitaka

2

這是存在至少在Rails的4.0.1的錯誤。

A pull request was proposed to fix it,但直到它被合併,你可以猴子補丁的Rails:

# config/initializers/extensions/postgres.rb 
module ActiveRecord 
    module ConnectionAdapters 
    class PostgreSQLColumn < Column 
     module Cast 
     private 

      def quote_and_escape(value) 
      case value 
      when "NULL" 
       value 
      else 
       "\"#{value.gsub(/(["\\])/, '\\\\\1')}\"" 
      end 
      end 
     end 
    end 
    end 
end 

旁註,我遇到了麻煩,因爲初始化沒有得到加載有在Rails的控制檯測試此。你可以這樣做有:

load "#{Rails.root}/config/initializers/extensions/postgres.rb" 
+0

該缺陷仍然存在於Rails的4.0.2。希望他們很快就會合並! – panmari