Reputation: 250
I understand how to store lots of datatypes in the model, but I'm not sure how to add an integer array.
Upvotes: 0
Views: 61
Reputation: 216
My understanding is that in general, it's not best to try and store arrays in the database, especially for data that doesn't really need to be in the array form. My suggestion would be to write some kind of helper method in your model that converts an array to a string of comma separated values using the join method (or even just converting the array to a string using to_s). You could then write a wrapper method for your model so that whenever you call the method to retrieve the array field, it converts the string result back into the array form. So all of the code necessary to handle converting to a string is abstracted, and the database representation is a little simpler. But yeah, serialize works too. Hope that helps!
Upvotes: 0
Reputation: 21791
You can use serialize
class YourModel < ActiveRecord::Base
serialize :array
end
ActiveRecord will automatically serialize the content of array to database and back.
Upvotes: 2