2012-03-19 81 views
2

active_record设置为使用PostgreSQL数据库。其中一列是character varying[](基本上是VARCHAR数组)。如何我插入一个数组使用ActiveRecord一个PostgreSQL表?

无论如何,我的导入例程然后读取制表符分隔的文本文件,并插入记录。一切都很好,直到我得到一个数组。应该转换为数组的列以逗号分隔。但该行本身是制表符分隔的。

我导入看起来像数据的样品(制表符分隔):

Col1  Col2  Col3     Col4 
---------------------------------------------- 
Apple  Pear  Sweet,Round,Green  Fruit 

COL3导入等(红宝石):col3.split(/,/)这使我在红宝石阵列。但active_record弹出:

PG::Error: ERROR: array value must start with "{" or dimension information (ActiveRecord::StatementInvalid)

如何正确插入该列?
此外,有时,COL3将是NULL。

+2

如果您确实想要本地Pg数组,请查看[ActiveRecordPostgresArray]( https://github.com/tlconnor/activerecord-postgres-array)扩展名。 – dbenhur 2012-03-19 04:31:29

回答

2

我可以使用下面的Ruby代码中插入:

alternatenames = '{' + s[3].split(/,/).map {|n| '"' + n + '"'}.join(",") + '}' 
+0

哇。得到了两个关于为我自己的问题提供答案的答案,它的工作方式与我想要的一样。我。询问的人。大声笑 – cbmeeks 2013-03-22 19:32:52

+3

你可能会因为你提出的解决方案而失败了,但它的工作原理并不像其他人想要的那样优雅。他们无法提出更好的解决方案并与您分享这一事实的事实可能与您的解决方案的质量有关。 – 2013-07-09 21:20:16

0

检查Postgres的这个文档:http://www.postgresql.org/docs/9.2/static/arrays.html

可以使用像数组实例化一个模型[:我,:符号]或[“我的”,“字符串”],但它(在我的经验,并形成什么,似乎在docs)将保存元素的字符串。

Search.create(tokens: [{hash: 'value'}, {test: "fails"}]) 
=> TypeError: can't cast Hash to string 

凡为:

[15] pry(main)> Search.create(tokens: [:G, :F]) 
=> #<Search id: 78, tokens: [:G, :F], created_at: "2013-12-18 06:29:36", updated_at: "2013-12-18 06:29:36"> 
[16] pry(main)> Search.last 
=> #<Search id: 78, tokens: ["G", "F"], created_at: "2013-12-18 06:29:36", updated_at: "2013-12-18 06:29:36"> 

在我的测试中,我有一个的搜索引擎,搜索和期限。

class SearchEngine < ActiveRecord::Base 
    has_and_belongs_to_many :terms 
    has_many :searches, through: :terms 
end 

class Term < ActiveRecord::Base 
    has_and_belongs_to_many :searches 
    has_and_belongs_to_many :searche_engines 
end 

class Search < ActiveRecord::Base 
    has_many :rankings 
    has_many :results, through: :rankings 
    has_and_belongs_to_many :terms 
    has_many :search_engines, through :terms 
end 


# These work: 
# these next two are the way postgrespl says to query against the array. You get the 
Search.where(tokens: '{A,B}') 
Search.where(tokens: '{C,D}').first_or_create 

[3] pry(main)> Search.where(tokens: ['C','D']).first 
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: array value must start with "{" or dimension information 

[4] pry(main)> Search.where(tokens: '{C,D}').first 
=> #<Search id: 77, tokens: ["C", "D"], created_at: "2013-12-18 06:27:24", updated_at: "2013-12-18 06:27:24"> 

term = "accident" 
Search.where("? = ANY (tokens)", term).first 
=> #<Search id: 8, tokens: ["accident", "prevention", "safety"], created_at: "2013-12-18 07:48:13", updated_at: "2013-12-18 07:48:13"> 
Search.create(tokens: [:Aortic, :Any, :Other, :Elements]) 
Search.where("'Aortic' = ANY (tokens)").first 
Parent.first.first_relationships.first.second_.where("'smelly' = ANY (tokens)").first 
# The next one will create one with an empty array for tokens and push it into Term.searches anyway. Same thing with 'smelly' 
Term.first.searches.where("':smelly' = ANY (tokens)").first_or_create do |s| Term.first.searches << s 
end 

# These error 
Search.where(tokens: "Aortic").first 
Search.where(tokens: [:Aortic, :Any, :Other, :Elements]).first 

另外,如果您有嵌套数组,你可以做一个地方搜索与此:“{{1,2,3},{4,5,6},{7,8,9}} '查找具有列值的行[[1,2,3],[4,5,6],[7,8,9]]

相关问题