Rails是否支持mysql json数据类型 [英] Does rails support mysql json data type

查看:112
本文介绍了Rails是否支持mysql json数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Rails中,我们可以将文本数据类型用于要向其保存哈希值或数组的mysql中的列,其中Rails以yaml格式序列化哈希并将其保存在列中.

I'm aware that in rails we can use a text data type for columns in mysql that we want to save hashes or array to it, where rails serialize the hash in yaml format and save it in the column.

class A < ActiveRecord::Base
  serialize :data, Hash
end

但是,如果需要在此列上执行一些搜索,则必须加载所有记录并反序列化所有哈希,然后使用ruby在哈希中搜索.那么有没有办法告诉mysql在散列中搜索并返回匹配的记录呢?我认为普通的yaml序列化不支持此功能,因为它只是文本,所以rails是否支持mysql json数据类型或此问题的任何其他解决方案?

However if I need to perform some search on this column, I have to load all records and de-serialize all the hashes, and use ruby to search within the hashes. So is there a way to tell mysql to search within the hashes and return matched records ? I think this is not supported with normal yaml serialization as it is just a text, so does rails support mysql json data type or any other solution to this problem ?

推荐答案

看起来Rails 5应该原生支持MySQL JSON数据类型.此处#21110 有一个拉取请求,对此进行了一些描述.

It looks like Rails 5 should have support for the MySQL JSON data type natively. There is a pull request here #21110 that describes it a bit.

看起来您可以将其添加到create_tablechange_table:

And it looks like you'll be able to add this to your create_table or change_table:

create_table :json_data_type do |t|
    t.json :settings
end

这篇关于Rails是否支持mysql json数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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