I'm sure this is trivial, but I've been banging my head against the desk for the last few hours. I'm trying to convert a string (i.e. "key1,key2:) to an array (i.e. ["key1","key2"]) before storing it in the database. I'm using a before_validation callback in my model and it doesn't seem to be firing.
My model looks like this:
class Product < ActiveRecord::Base
serialize :keywords, Array
attr_accessible :keywords
before_validation :update_keywords
def update_keywords
self.update_attributes(:keywords, self.keywords.split(',').collect(&:strip)
end
end
I'm getting a SerializationTypeMismatch error. Obviously, either the update_keywords method isn't being run or isn't properly returning the updated attributes.
Any ideas?
EDIT
I'm using Rails 3.0.3, if that makes any difference.
EDIT #2
Just wanted to follow up and say that I've found that removing the serialized columns type declaration and ensuring that it defaults to an empty array (i.e. []) rather than nil clears up many issues.
For the sake of anyone like myself just beginning their journey with Rails, I should note that this most likely not the best way to go about creating serialized attributes. I'm just porting over a project that utilizes an old database.