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.

Now explicit serializated objects retuns new objects when nil

#1736

Now explicit serializated objects retuns new objects when nil. I think these is the expected behavior


    class Topic < AR::Base
      serialize :content
      serialize :preferences, Hash
      serialize :properties, OpenStruct
    end
    
    t = Topic.new
    t.content.nil? => #true
    t.preferences # => Hash.new
    t.preferences[:sex] = true
    t.properties.rails = 3

Reported by Guillermo Álvarez · January 12th, 2009 @ 01:03 PM

State: committed
Milestone: 3.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Manfred Stienstra
    Manfred Stienstra

    This would mean that you can't save nil values for serialized attributes with a specific class anymore. Maybe an extra option :allow_nil or something would be a good solution?

    January 12th, 2009 @ 01:16 PM

  2. Guillermo Álvarez
    Guillermo Álvarez

    No. These patch don't change the default behavior.

    When you do something like these

    
      serialize :properties
    

    The behaviour is the same. You can set nil to properties.

    But if you say that properties is a hash, the empty value for a hash is an empty Hash.

    
    class Topic < AR::Base
      serialize :properties, Hash
    end
    
    t = Topic.new
    t.properties['name'] = 'Guillermo'
    t.save
    
    t.properties.clear
    t.save
    

    Obviously you don't have to think or initialize an uninizialize hash, openstruc, array, etc...

    These reason of these ticket is that i have always do something like these

    
    class User < AR::Base
      serialize :preferences, OpenStruct
      def preferences
        read_attribute :preferences || OpenStruct.new
      end
    

    I think about add something like allow_nil but i don't found any case where a prefere to save a nil value instead of a empty Hash, Array, OpenStruct.

    January 12th, 2009 @ 01:45 PM

  3. Pratik
    Pratik
    • Assigned user set to Michael Koziarski

    Looks good to me.

    March 10th, 2009 @ 05:13 PM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    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.

    February 2nd, 2011 @ 04:41 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:42 PM

  7. Guillermo Álvarez
    Guillermo Álvarez
    • State changed from stale to open

    [state:open]

    Update patch for rails 3.x

    • Added some tests
    • Added documentation

    February 3rd, 2011 @ 12:25 AM

  8. Aaron Patterson
    Aaron Patterson
    • State changed from open to committed

    Applied and pushed. Thanks!

    February 3rd, 2011 @ 05:09 PM