13

I have an active record model:

class Person < ActiveRecord::Base
  serialize :tags, Array
end

and in the migration the tags column is declared as

t.text :tags, :default => []

but when I try to create a person

Person.new

I get the error

ActiveRecord::SerializationTypeMismatch: added was supposed to be a Array, but was a String

How do I set the default to be an empty array in the migration?

NB: I know I could do this using after_initialize but I prefer to set defaults in migrations

5
  • 1
    Instead of after_initialize, try the default_value_for plugin: github.com/FooBarWidget/default_value_for Commented Feb 27, 2011 at 16:27
  • Note that you can't set a default on a TEXT column in MySQL, and potentially others. Commented Feb 27, 2011 at 16:36
  • coreyward: interesting, I didn't know about that. It looks like it's only a problem on windows though bugs.mysql.com/bug.php?id=25520 Commented Feb 27, 2011 at 16:42
  • paul: the default_value_for plugin looks quite tidy, if I can't find a way to do it with migrations then I think I'll go that route Commented Feb 27, 2011 at 16:44
  • I the same problem on a string column which I serialize as Array. An empty array would result in an empty string "". The problem was that the column had null: false and serialize wants to serialize [] as nil. When it cannot, it uses "", but then it can't deserialize correctly. Commented May 18, 2017 at 10:06

3 Answers 3

8

There is an option to specify the class you want to store objects as. Try this:

class Person < ActiveRecord::Base
  serialize :tags, Array
end
Sign up to request clarification or add additional context in comments.

2 Comments

ah, good catch, that should've been in the original post. I do already have this specified, no dice.
In the latest Rails, you specify it with type:, so serialize :tags, type: Array.
4

It sounds rather like you've hit a framework bug or something else is interfering with your migration; I just tried building the above with Rails 2.3.10 and can instantiate objects without problems. However, I note that YAML is used for serialisation, so:

t.text :tags, :default => [].to_yaml

...might do the trick. Both migrations seemed to behave equally in my test application.

2 Comments

yeah, I gave that one a go, it gets turned into "" in the schema instead of "--- []\n\n"
Since I can't replicate this fault on Rails 2.3, it's either a Rails 3 problem or something wider up with your application. Just in case comments elsewhere about text columns are on the mark, though, have you tried using "t.string" instead of "t.text", with all other code as originally written, just as an experiment? Oh, and have you tried without the "Array" class specifier on the 'serialize' call?
3

I had a similar issue and solved it by removing the default value. ActiveRecord will treat nil as [] when you begin to add values to the array.

Migration:
t.text :tags

Model:
class Person < ActiveRecord::Base
  serialize :tags, Array
end

Usage:
p = Person.new
p.tags << "test" 

This works because Rails will treat nil as [] for the array.

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.