I’ve got a serialized array
class Enviroment < ActiveRecord::Base
serialize :roles, Array
...
t.column :roles, :string, :default=> Array.new
and I would like do things like
Enviroment.find(1).roles.push 'thing'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Providing such default value will save it’s YAML representation as String into the Database and Rails unserializes that back into string. That’s why it is not working.
Also, maybe you’d want to use
textas the column type, so that your array won’t be truncated when it gets longer. In recent Rails syntax, that would be:And when you want to store new role into database, you’d have to save the object afterwards (in contrast to
has_manyassociation which is automatically saved b/c it’s key is in the other table):Or if it absolutely needs to be oneliner: