I’ve recently added a ‘preferences’ field for my users which uses thw ActiveRecord serialize method in my rails app. This works fine for existing users but when I try initializing a new user model in the console I get an error:
u = User.new
#=>
ActiveRecord::SerializationTypeMismatch: preferences was supposed to be a Hash, but was a String
I’ve added the field like this in the migration:
add_column :users, :preferences, :text, :default => { :allows_public_contributions => false }.to_yaml
And in the model, user.rb:
serialize :preferences, Hash
Any ideas?
Rails handles the serialization by itself. Remove the
to_yamlmethod call from the default option in your migration. If you run this method on a hash you get a string.That is the reason why you get the exception.