Reputation: 5765
What are the pros and cons of setting the Postgres column as array in a migration vs using a string field with Serialize in a rails Model?
t.string :tags, array: true, default: []
vs
# Serialize a preferences attribute
class User < ActiveRecord::Base
serialize :tags
end
This post mentioned some good notes on the topic, but would like more views: New data not persisting to Rails array column on Postgres Rails 4 field type for multiselect with predefined values
Upvotes: 1
Views: 1096
Reputation: 8429
You can query and manipulate native PostgreSQL Array type using pure SQL whereas when using serialized attribute, querying and manipulating using SQL might be cumbersome and not as performant as native array type.
However if you plan or need the database technology to be interchangeable, using serialized attribute is database agnostic.
Upvotes: 3