2

According to the Rails 5 documentation an empty Array should be serialized to NULL:

Empty objects as {}, in the case of Hash, or [], in the case of Array, will always be persisted as null.

Maybe I got it wrong but my empty Array is always serialized as --- [] (YAML) or [] (JSON) in the database. Beside that, the serialization works fine and as expected.

I am asking because we are currently running Rails 4 but we may want to switch to Rails 5. But then the behavior would change from [] to NULL (since the sentence above is missing in Rails 4 documentation). Therefore I thought of writing my own serializer for Rails 4 that also uses NULL instead of [], but it looks like as if this is not necessay.

For completeness:

class TestModel < ActiveRecord::Base  
    serialize :test_attribute, JSON #doesn't matter if it's JSON, Hash or YAML
end

TestModel.create(test_attribute: [])

Any ideas?

0lli.rocks
  • 1,027
  • 1
  • 18
  • 31

0 Answers0