在迁移中将Postgres列设置为数组与在rails模型中使用带有Serialize的字符串字段的优缺点是什么?
t.string :tags, array: true, default: []
vs
# Serialize a preferences attribute
class User < ActiveRecord::Base
serialize :tags
end
这篇文章提到了有关该主题的一些很好的注释,但希望有更多的见解: New data not persisting to Rails array column on Postgres Rails 4 field type for multiselect with predefined values
答案 0 :(得分:1)
您可以使用纯SQL查询和操作本机PostgreSQL数组类型,而在使用序列化属性时,使用SQL进行查询和操作可能很麻烦,并且不如本机数组类型执行。
但是,如果您计划或需要数据库技术可互换,则使用序列化属性与数据库无关。