#624 stale
Bryce Kerley

Columns marked for serialization do not serialize

Reported by Bryce Kerley | July 15th, 2008 @ 06:54 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.

  • Joshua Peek

    Joshua Peek October 28th, 2008 @ 04:31 PM

    • → State changed from “new” to “stale”

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

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

People watching this ticket

Attachments