So I'm trying to convert a string that I get from a hidden_field to an array before I save it to the database and I'm having no luck. My Active record datatype is:
add_column :contractors, :regions, :text, array: true
My model looks like this
class Contractor < ActiveRecord::Base
before_validation :make_array
private
def make_array
self.regions = self.regions.split(',')
end
end
and I'm getting the value from a hidden field
<%= f.hidden_field :regions, value: "1,2,3" %>
It seems if I have array: true on database column self.regions is an empty array when the callback runs. If I remove array:true the string is converted to an array with the callback but it won't save to the database (can't cast Array to text). I've tried adding serialize: :regions but I get this error:
ERROR: array value must start with "{" or dimension information.
I've also tried sending an array from the hidden field with no luck.
Any ideas what I am doing wrong?