This project is archived and is in readonly mode.

#1736 ✓committed
Guillermo Álvarez

Now explicit serializated objects retuns new objects when nil

Reported by Guillermo Álvarez | January 12th, 2009 @ 01:03 PM | in 3.x

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

Comments and changes to this ticket

  • Manfred Stienstra

    Manfred Stienstra January 12th, 2009 @ 01:16 PM

    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?

  • Guillermo Álvarez

    Guillermo Álvarez January 12th, 2009 @ 01:45 PM

    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.

  • Pratik

    Pratik March 10th, 2009 @ 05:13 PM

    • Assigned user set to “Michael Koziarski”

    Looks good to me.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:41 PM

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

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:42 PM

    • State changed from “open” to “stale”
  • Guillermo Álvarez

    Guillermo Álvarez February 3rd, 2011 @ 12:25 AM

    • State changed from “stale” to “open”

    [state:open]

    Update patch for rails 3.x

    • Added some tests
    • Added documentation
  • Aaron Patterson

    Aaron Patterson February 3rd, 2011 @ 05:09 PM

    • State changed from “open” to “committed”

    Applied and pushed. Thanks!

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