Following some other StackOverflow questions and other articles on the web, I was trying to add an array attribute to one of my models.
rails g migration add_new_attribute_to_my_model new_attribute:text
Then in the migration file
def change
add_column :my_model, :new_attribute, :text, default: [].to_yaml
end
(because if I do
def change
add_column :my_model, :new_attribute, :text, default: [], array:true
end
I get TypeError: can't quote Array
in the migration).
rake db:migrate
In my_model_controller.rb
class MyModelController < ApplicationController
class MyModel < ActiveRecord::Base
serialize :new_attribute,Array
end
...
end
But like this I get
a = MyModel.new
=>MyModel ... new_attribute: "--- []\n">
a.new_attribute << "asd"
=>MyModel ... new_attribute: "--- []\nasd">
Where should I fix the process?