哈希数组用ActiveRecord [英] Array of Hashes with ActiveRecord

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

问题描述

ActiveRecord的(Rails的4.0)支持PostgreSQL的Hstore和Array数据类型,所以哈希值的数组在理论上是可能的,但我实现抛出:

  PG :: InvalidTextRe presentation:错误:格式不正确的数组文字:
 

该错误是显而易见的(双引号冲突):

<$p$p><$c$c>"{"null"=>"false","name"=>"schema_id","type"=>"integer","null"=>"false","name"=>"title","type"=>"text"}" :INSERT INTO实体(attribute_hash,schema_id,标题)VALUES($ 1,$ 2,$ 3)再次身份证

该解决方案并不明显给我,我怎么能实现呢?

我的架构:

  CREATE_TABLE:模式做| T |
  t.text:标题
  t.timestamps
结束

CREATE_TABLE:实体做| T |
  t.integer:schema_id,空:假的
  t.text:标题,空:假的
  t.hstore:attribute_hash,数组:真
结束
 

我的种子:

  @schema_id = Schema.create(标题:accreu')!
Entity.create!(
  schema_id:@ schema_id.id,标题:实体,
  attribute_hash:
    {空:假的,名称:schema_id,类型:整数},
    {空:假的,名称:名称,类型:文本}
   ]
)
 

解决方案

这是被固定在滑轨一个确认的错误提交的 7c32db1 ,这是present的版本4.1.0.rc1及更高版本。

ActiveRecord (Rails 4.0) supports PostgreSQL Hstore and Array datatypes, so an Array of Hashes is theoretically possible, but my implementation throws:

PG::InvalidTextRepresentation: ERROR:  malformed array literal:

The error is obvious (double quote conflict):

"{"null"=>"false","name"=>"schema_id","type"=>"integer","null"=>"false","name"=>"title","type"=>"text"}"

: INSERT INTO "entities" ("attribute_hash", "schema_id", "title") VALUES ($1, $2, $3) RETURNING "id"

The solution is not obvious to me, how can I implement this?

My schema:

create_table :schemas do |t|
  t.text    :title
  t.timestamps
end

create_table :entities do |t|
  t.integer :schema_id,       null: false
  t.text    :title,           null: false
  t.hstore  :attribute_hash, array: true
end

My seed:

@schema_id = Schema.create!(title: 'accreu')
Entity.create!(
  schema_id: @schema_id.id, title: 'entities',
  attribute_hash: [
    {null: "false", name: :schema_id, type: :integer},
    {null: "false", name: :title, type: :text}
   ]
)

解决方案

This is a confirmed bug in Rails that was fixed in commit 7c32db1, which is present in versions 4.1.0.rc1 and later.

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

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