Ruby on rails Rails迁移以添加对象数组类型的列

Ruby on rails Rails迁移以添加对象数组类型的列,ruby-on-rails,migration,Ruby On Rails,Migration,我想添加一列对象数组类型 我被困在移民中 添加列:表名称,:列名称 写什么来代替……添加一列数据类型文本 add_column :table_name, :column_name, :text, default: "" #Field type should be text to store array of hashes 在模型中,将其序列化以将其转换为数组 class TableName < ActiveRecord::Base serialize :column_name, Ar

我想添加一列对象数组类型

我被困在移民中

添加列:表名称,:列名称


写什么来代替……

添加一列数据类型文本

add_column :table_name, :column_name, :text, default: "" #Field type should be text to store array of hashes
在模型中,将其序列化以将其转换为数组

class TableName < ActiveRecord::Base
  serialize :column_name, Array
end

希望这将帮助您添加带有数据类型文本的列

add_column :table_name, :column_name, :text, default: "" #Field type should be text to store array of hashes
在模型中,将其序列化以将其转换为数组

class TableName < ActiveRecord::Base
  serialize :column_name, Array
end

希望这将帮助您

如果您使用PostgreSQL,您可以尝试不同的解决方案,例如:

数组列类型 或者您可以尝试将数据存储为JSON 并更灵活地解析json,以生成数组和其他结构


更多信息请访问

如果您使用的是PostgreSQL,您可以尝试不同的解决方案,例如:

数组列类型 或者您可以尝试将数据存储为JSON 并更灵活地解析json,以生成数组和其他结构


有关详细信息,请参见

您正在使用的数据库?postgres、mysql或mongodb?@G.B使用postgresdatabase@ashvin答案似乎是正确的。您还可以在hstore上查看postgres,其中存储哈希。您正在使用哪个数据库?postgres、mysql或mongodb?@G.B使用postgresdatabase@ashvin答案似乎是正确的。您还可以查看存储散列的postgres的hstore create_table :table_name do |t| t.json 'json_column_name' end # Usage YoutModel.create(json_column_name: { key1: "val1", key2: ["val21", "val22"]})
add_column :table_name, :column_name, :string, array: true