Sql Activerecord,其中包含一个哈希值

Sql Activerecord,其中包含一个哈希值,sql,ruby-on-rails,ruby,activerecord,Sql,Ruby On Rails,Ruby,Activerecord,我有一个城市模型,其中有一个缓存的_info字段,它是一个序列化的散列 {:population=>20000, :more_stuff =>....} 如果我在Activerecord中进行以下查询 City.where('cached_info[:population] > 300').count 我回来了 ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL synt

我有一个城市模型,其中有一个缓存的_info字段,它是一个序列化的散列

{:population=>20000, :more_stuff =>....}
如果我在Activerecord中进行以下查询

City.where('cached_info[:population] > 300').count
我回来了

ActiveRecord::StatementInvalid: Mysql2::Error: 
You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near '[:population] > 300)' at line 1: SELECT COUNT(*) FROM `places` WHERE `places`.`type` = 'City' AND (cached_info[:population] > 3)

有人对此有解决办法吗?

通过ActiveRecord和SQL在序列化散列中进行查询没有简单的方法,除非在查询中使用类似于
(但这无法进行类似于
的比较,就像JIT所说的那样,类似于/ILIKE的方法是可行的,例如:

City.where('cached_info LIKE ?', '%name: Louisiana%')

如果您使用PostgreSQL和JSONB字段来存储哈希值,您可以在
中的
中下拉到SQL,并使用如下内容:

City.where("cast(cached_info ->> 'population' as int) > ?", 300)

->
是一个Postgres-JSONB访问操作符--.

以后不要在数据库中存储散列;)辛苦学习,是吗?