Reputation: 17900
I have an array field in one of my models, with elements being symbols. For example, here is how I assign values to this field:
Model.field = [:a, :b, :c]
I use Postgres so I store these arrays in an array column. The problem is that Rails automatically serializes the symbols from the given array to strings when saving them to the database, but doesn't convert them back to symbols when objects are fetched from the database. How can I tell my model to automatically convert array values to symbols?
Upvotes: 2
Views: 1217
Reputation: 20614
You could try overriding the attribute reader method and doing the datatype conversion there
class Model < ActiveRecord::Base
# ...
def field
self[:field].map(&:to_sym)
end
end
Upvotes: 1