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.

Fixtures with serialized attributes are not properly loaded

#6004

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?

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

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

    November 18th, 2010 @ 04:08 PM

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

    November 18th, 2010 @ 08:37 PM

  3. kelyar
    kelyar

    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

    November 30th, 2010 @ 08:29 AM

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

    March 21st, 2011 @ 12:00 AM

  5. rails
    rails
    • State changed from open to stale

    March 21st, 2011 @ 12:00 AM