1

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

2 Answers 2

2

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"]
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.