在哈希中保存错误排序后的 activerecord-postgres-hstore [英] activerecord-postgres-hstore after save error ordering in Hash

查看:52
本文介绍了在哈希中保存错误排序后的 activerecord-postgres-hstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用Hstore存储为Hash Table,保存后Hash排序错误

class Service < ActiveRecord::Base
  serialize :properties, ActiveRecord::Coders::Hstore
end

service = Service.new
service.properties = { "aaa" => 1, "zz" => 2, "cc" => 3, "d" => 4 }
#=> { "aaa" => 1, "zz" => 2, "cc" => 3, "d" => 4 }
service.save
reload!
service = Service.find(:id)
service.properties
#=> { "d" => "4", "cc" => "3", "zz" => 2, "aaa" => 1 }
Bug::: wrong ordering after save

是不是因为序列化后按Tree排序.任何想法或任何人之前都遇到过这个问题?提前致谢.

Is it because after serialize that it orders by Tree. Any ideas or anyone had faced this problem before? Thanks in advance.

推荐答案

来自 精美的 PostgreSQL 手册:

F.16.hstore
[...]
该模块实现了 hstore 数据类型,用于在单个 PostgreSQL 值中存储键/值对集.
[...]
对的顺序并不重要(并且可能不会在输出中重现).

F.16. hstore
[...]
This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL value.
[...]
The order of the pairs is not significant (and may not be reproduced on output).

所以 PostgreSQL 的 hstore 类型是一组无序的键/值对,不保证键/值对的任何特定顺序.一旦您的 Ruby Hash 转换为 hstore,排序就会丢失.

So PostgreSQL's hstore type is an unordered set of key/value pairs that doesn't guarantee any particular order of the key/value pairs. Once your Ruby Hash is converted to an hstore, the ordering is lost.

如果您需要维护哈希中的顺序,则必须使用不同的序列化格式.

If you need to maintain the order in your Hash you'll have to use a different serialize format.

这篇关于在哈希中保存错误排序后的 activerecord-postgres-hstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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