Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Columns marked for serialization do not serialize

#624

Columns in an AR model can be marked for serialization if they are being used to store Arrays, Hashes, or other YAML-able data non-relationally:

class Hat < ActiveRecord::Base
  serialize :description
end

However, if this column is used for a variant/union type (such as a column used sometimes for strings, sometimes for arrays, and sometimes for hashes), data can become corrupted when it is deserialized on its way out of the database:

  1) Failure:
test_what_goes_in_comes_back_out(HatTest)
    [./test/unit/hat_test.rb:10:in `test_what_goes_in_comes_back_out'
     /Users/bryce/Documents/serialize_test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
     /Users/bryce/Documents/serialize_test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `run']:
<"<span style=\"color: #800 FileStore can only store strings\">SODA HAT IS A SLANG TERM FOR A LID</span>\n"> expected but was
<{"<span style=\"color"=>nil}>.

A project demonstrating this can be found at: http://github.com/bkerley/serial...

The attached patch adds a test for this problem and provides a fix that passes rake test_sqlite3.

Reported by Bryce Kerley · July 15th, 2008 @ 06:55 PM

State: stale
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Bryce Kerley
    Bryce Kerley

    So it turns out that attributes_with_quotes grabs the unserialized version through the provided accessor, and then re-serializes them into the database. http://github.com/bkerley/serial... adds a test displaying this case. I'm still cooking up a fix for this.

    July 15th, 2008 @ 07:38 PM

  2. Bryce Kerley
    Bryce Kerley

    Can't do this the way I'm doing it without creating stupid workarounds for lots of tests.

    July 15th, 2008 @ 09:58 PM

  3. Bryce Kerley
    Bryce Kerley
    • Tag changed from activerecord, bug, patch, serialization, tested, yaml to activerecord, bug, patch, serialization, yaml

    OK, got this most of the way fixed, except I'm failing a couple validation tests:

      1) Failure:
    test_validate_uniqueness_scoped_to_defining_class(ValidationsTest)
        [./test/cases/validations_test.rb:393:in `test_validate_uniqueness_scoped_to_defining_class'
         ./test/cases/../../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
         ./test/cases/../../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `run']:
    Saving r2.
    <false> is not true.
    
      2) Failure:
    test_validate_uniqueness_with_scope(ValidationsTest)
        [./test/cases/validations_test.rb:376:in `test_validate_uniqueness_with_scope'
         ./test/cases/../../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
         ./test/cases/../../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `run']:
    Saving r2 first time.
    <false> is not true.
    
    

    The changed code, tests, etc. are in the git repo: http://github.com/bkerley/serial...

    July 17th, 2008 @ 03:10 PM

  4. Murray Steele
    Murray Steele

    +1

    The breakages you are seeing were already there. Even without your patch you can't validate uniqueness against serialized attributes. It's just that before a string wouldn't be saved as a yamlized string so the find would work. (E.g. with/without your patch it does select * from whatever where whatever.serialized_attribute = 'a string'; whereas what's in the DB is '---\n "a string"'.)

    Either:

    1. we fix the tests to not test validation of uniqueness against serialized attributes

    2. we fix validates_uniqueness of to work with serialized attribtues

    3. we fix finders to know about serialized attributes and co-erce values accordingly.

    1 or 3 work for me.

    July 23rd, 2008 @ 04:13 PM

  5. josh
    josh
    • State changed from new to stale

    Staling out, please reopen if this is still a problem.

    October 28th, 2008 @ 04:31 PM