Does rails support mysql json data type

核能气质少年 提交于 2019-12-12 09:37:05

问题


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

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 ?


回答1:


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.

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


来源:https://stackoverflow.com/questions/35769941/does-rails-support-mysql-json-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!