3

I'm looking to add a row to my "posts" database that can store an array that holds multiple user id's. I noticed that none of the other questions asked on here were using rails 4. I think that this is going to require me to serialize the information but I'm not sure how to do this.

thanks in advance!

1
  • Did you try using to_json to serialize the array? Commented Jan 8, 2015 at 17:05

1 Answer 1

5

Create a migration

rails g migration AddSomethingToPosts something:text

In your model add

serialize :something, Array

here is more info: http://apidock.com/rails/ActiveRecord/AttributeMethods/Serialization/ClassMethods/serialize

also you can use store -> http://api.rubyonrails.org/classes/ActiveRecord/Store.html, and it probably will be a better choice :)

Sign up to request clarification or add additional context in comments.

6 Comments

What makes store be the better choice. thanks for the input and links
How would I go about appending to the end of the array?
Store is a wrapper around serialize, giving you easy way to create accossors in case you want something more than a simple array. To append use @post.something << value, in your controller.
Do I have to use store to use that append method?
don't think so, serialize should do fine :), here stackoverflow.com/questions/9485807/…
|

Your Answer

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