This project is archived and is in readonly mode.

#6004 ✓stale
C. Bedard

Fixtures with serialized attributes are not properly loaded

Reported by C. Bedard | November 18th, 2010 @ 04:00 PM

Having a model with serialized attributes (as an Array or Hash) doesn't allow to create YAML fixtures for them. Example:

class User
  serialize :preferences, Hash
end

And a corresponding fixture:

User1:
  id: 1
  preferences:
    sort: 'name'
    lines: 50

When loading that fixture (which does not instantiate ActiveRecord model), such values are simply be inserted as Hash#to_s instead of Hash#to_yaml, causing the ActiveRecord class to complain when instantiating those records from the database (Expected Hash, got String).

This is a change in behaviour from Rails 2.3, which converts such values to YAML. The change was introduced in ActiveRecord::ConnectionAdapters::Quoting (file active_record/connection_adapters/abstract/quoting.rb) where the quote method simply calls value.to_s, whereas the same method in Rails 2.3 would call value.to_yaml.

Is there a reason why Objects are no longer serialized as yaml, but simply converted to String when quoted for SQL insertion?

Comments and changes to this ticket

  • C. Bedard

    C. Bedard November 18th, 2010 @ 04:08 PM

    • Tag changed from quoting serialize array hash to array, hash, quoting, serialize
  • C. Bedard

    C. Bedard November 18th, 2010 @ 08:37 PM

    • Tag changed from array, hash, quoting, serialize to array, fixtures, hash, quoting, serialize
  • kelyar

    kelyar November 30th, 2010 @ 08:29 AM

    For those who still suffers from it - I found quite strange workaround here: http://code.alexreisner.com/articles/serialized-fixtures-in-rails-3...

    The idea is to use <%= {}.to_yaml.inspect %> in your fixtures.
    tested on 3.0.3

  • rails

    rails March 21st, 2011 @ 12:00 AM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails March 21st, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

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>

Pages