This project is archived and is in readonly mode.

#624 ✓stale
Bryce Kerley

Columns marked for serialization do not serialize

Reported by Bryce Kerley | July 15th, 2008 @ 06:55 PM | in 2.x

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\">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.

Comments and changes to this ticket

  • Bryce Kerley

    Bryce Kerley July 15th, 2008 @ 07:38 PM

    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.

  • Bryce Kerley

    Bryce Kerley July 15th, 2008 @ 09:58 PM

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

  • Bryce Kerley

    Bryce Kerley July 17th, 2008 @ 03:10 PM

    • 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...

  • Murray Steele

    Murray Steele July 23rd, 2008 @ 04:13 PM

    +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.

  • josh

    josh October 28th, 2008 @ 04:31 PM

    • State changed from “new” to “stale”

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages