Reputation: 9472
I am trying to add an array to my model "User"
serialize :year, Array
How do I add this attribute to my model? I see most places saying to just write this into the Users.rb file, but then there is never a column. Am I missing something?
Also, How do I access it from my controller? Thanks
Upvotes: 1
Views: 185
Reputation: 2318
You must have a column in your database. Then serialize the object and no need to mention the type of the object, if you are not sure. It is as follows :
serialize :year
If you assign hash it will be hash object or you assign array it will be Array object.
Upvotes: 1
Reputation: 29349
You have to add the column year
to database as text
column
add_column :users, :year, :text
You can access it like normal array.
Eg:
user = User.new(:year => ["2012", "8", "22"])
user.year #=> ["2012", "8", "22"]
Upvotes: 2