【发布时间】:2015-06-03 05:20:53
【问题描述】:
我的表 table_1 中有一个哈希列 hash_column。
在table_1.rb中
serialize :hash_column, Hash
列中存储的值是
hash_column
record1 => { index1 => '2'}
record2 => { index2 => '3' }
record3 => { index1 => '4'}
我想获取 hash_column 的键为 index1 的所有记录。 我现在的做法是
table_1.where("hash_column LIKE ?", "%index1%" )
这很好用。但是在 Rails 中,有没有一种正确的方法来使用哈希列的键和值来查询它?
ps:我用的是mysql数据库
【问题讨论】:
-
嘿!检查此article。希望对您有所帮助!
-
如果你使用 Postgres,有 Hstore 非常适合存储 Hash 数据,我们可以像使用 activerecord 一样轻松查询它们。schneems.com/post/19298469372/…
-
我想用mysql数据库来实现。
标签: mysql ruby-on-rails ruby hash rails-activerecord