我可以存储阵列中hstore使用Rails [英] Can I store arrays in hstore with Rails

查看:150
本文介绍了我可以存储阵列中hstore使用Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保存这样的数据:

User.create(名称:盖伊,属性:{URL:为url1,URL2,URL3],STREET_ADDRESS:ASDF})

我可以在Rails的4这样做呢?到目前为止,我曾尝试迁移:
add_column:用户:性能:hstore,数组:真正的

Can I do so in Rails 4? So far, I have tried migration: add_column :users, :properties, :hstore, array: true

但是,当我保存hstore的数组,它返回错误:

But when I save the array in hstore, it returns error:

PG :: InvalidTextRe presentation:错误:数组值必须以{或维度信息

推荐答案

hstore 适用于简单的键/值存储,其中两个键和值是简单的非结构化字符串。从精细的手工

hstore is intended for simple key/value storage, where both the keys and values are simple unstructured strings. From the fine manual:

F.16。 hstore

本模块实现了商店数据类型为单个PostgreSQL的值内存储集中的键/值对。 [...]键和值仅仅是文本字符串。

This module implements the store data type for storing sets of key/value pairs within a single PostgreSQL value. [...] Keys and values are simply text strings.

请注意最后一句:键和值在 hstore 都是字符串。这意味着,你不能把在 hstore 值数组没有一些手把手的数组转换和从一个字符串,你真的不希望被乱搞用之类的事情。

Note the last sentence: keys and values in hstore are strings. That means that you can't put an array in an hstore value without some handholding to convert the array to and from a string and you really don't want to be messing around with that sort of thing.

然而,有一个 JSON数据类型使用

8.14。 JSON类型

在RFC 4627中指定的 JSON 数据类型可以用来存储JSON(JavaScript对象符号)数据。

The json data type can be used to store JSON (JavaScript Object Notation) data, as specified in RFC 4627.

和JSON可以轻松地处理嵌入的数组和对象。尝试使用JSON来代替:

and JSON can easily handle embedded arrays and objects. Try using JSON instead:

add_column :users, :properties, :json

您将不得不删除旧的 hstore 列第一,但。

You'll have to remove the old hstore column first though.

此外,你不想数组:真正的 hstore 列作为你没有存储的阵 hstore S,你只是想其中的一个。

Also, you didn't want array: true on your hstore column as you weren't storing an array of hstores, you just wanted one of them.

这篇关于我可以存储阵列中hstore使用Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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