百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

activerecord-postgres-hstore

> 编程语言
开源

再见 serialize, 你好 hstore。加快数据库中的哈希速度。

830 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

再见 serialize, 你好 hstore。加快数据库中的哈希速度。

DEPRECATED - Just use Rails JSON or HStore built-in support.

If you are using Rails 4 you don't need this gem as ActiveRecord 4 provides HStore type support out of the box. ActiveRecord will see your HStore column and do all of the work for you. Additional code is no longer needed.

You can test it with a migration like this:

ruby
class CreateTest  t = Test.new data: {a: 1, b:2}
=> #"1", "b"=>"2"}>
irb(main):004:0> t.save!
   (0.3ms)  BEGIN
  SQL (2.3ms)  INSERT INTO "tests" ("data") VALUES ($1) RETURNING "id"  [["data", "\"a\"=>\"1\",\"b\"=>\"2\""]]
   (0.5ms)  COMMIT
=> true
irb(main):005:0> t
=> #"1", "b"=>"2"}>
irb(main):006:0> t.data
=> {"a"=>"1", "b"=>"2"}
irb(main):007:0> t.data['a']
=> "1"

For more information take a look here

Common use cases

Add settings to users, like in rails-settings or HasEasy.

ruby
class User  person = Person.new
=> #
irb(main):002:0> person.data['favorite_color'] = 'blue'
=> "blue"

Querying the database

Now you just need to learn a little bit of new sqls for selecting stuff (creating and updating is transparent). Find records that contains a key named 'foo’:

ruby
Person.where("data ? 'foo'")

Find records where 'foo’ is equal to 'bar’:

ruby
Person.where("data -> 'foo' = 'bar'")

This same sql is at least twice as fast (using indexes) if you do it that way:

ruby
Person.where("data @> 'foo=>bar'")

Find records where 'foo’ is not equal to 'bar’:

ruby
Person.where("data -> 'foo' <> 'bar'")

Find records where 'foo’ is like 'bar’:

ruby
Person.where("data -> 'foo' LIKE '%bar%'")

If you need to delete a key in a record, you can do it that way:

ruby
person.destroy_key(:data, :foo)

This way you’ll also save the record:

ruby
person.destroy_key!(:data, :foo)

The destroy_key method returns 'self’, so you can chain it:

ruby
person.destroy_key(:data, :foo).destroy_key(:data, :bar).save

But there is a shortcuts for that:

ruby
person.destroy_keys(:data, :foo, :bar)

And finally, if you need to delete keys in many rows, you can:

ruby
Person.delete_key(:data, :foo)

and with many keys:

ruby
Person.delete_keys(:data, :foo, :bar)

Caveats

hstore keys and values have to be strings. This means true will become "true" and 42 will become "42" after you save the record. Only nil values are preserved.

It is also confusing when querying:

ruby
Person.where("data -> 'foo' = :value", value: true).to_sql
#=> SELECT "people".* FROM "people" WHERE ("data -> 'foo' = 't'") # notice 't'

To avoid the above, make sure all named parameters are strings:

ruby
Person.where("data -> 'foo' = :value", value: some_var.to_s)

Have fun.

Test Database

To have hstore enabled when you load your database schema (as happens in rake db:test:prepare), you have two options.

The first option is creating a template database with hstore installed and set the template option in database.yml to that database. If you use the template1 database for this you don't even need to set the template option, but the extension will be installed in all your databases from now on by default. To install the extension in your template1 database you could simply run:

ruby
psql -d template1 -c 'create extension hstore;'

The second option is to uncomment or add the following line in config/application.rb

ruby
config.active_record.schema_format = :sql

This will change your schema dumps from Ruby to SQL. If you're unsure about the implications of this change, we suggest reading this Rails Guide.

Help

You can use issues in github for that. Or else you can reach us at twitter: @dbiazus or @joaomilho

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Juan Maiz. See LICENSE for details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言