Rails 3.1 custom serialization and it does not clone correctly #3

Closed
amalagaura opened this Issue Apr 12, 2012 · 10 comments

Comments

Projects
None yet
2 participants

This gem is amazing. This is some serious meta-fu. Is very impressive.

I am using a custom serialization class in one of my models and I am not getting a correct clone.

This is probably some bad programming but I have a class to store an array of numbers as a sorted comma separated string. I don't think it is cloning correctly.

  class ArrayPack
    def load(array_or_text)
      array_or_text = array_or_text.split(',').collect(&:to_i) if array_or_text.class == String
      return [] if array_or_text.blank?
      raise "Need to search a string or array, you passed #{array_or_text}" unless array_or_text.class == Array
      array_or_text.collect(&:to_i).sort
    end

    def dump(int_array)
      self.load(int_array).join(',')
    end
  end

  serialize :path, ArrayPack.new
Contributor

rocksolidwebdesign commented Apr 12, 2012

Hi, thanks for the report... can you give me a little more context as to how this is being used? A simple test case perhaps? I'm mostly curious where this fits in to the context of your actual ActiveRecord model.

Glad you like the gem, and hopefully we can get this resolved for you ASAP :)

Yes let me do some more digging. I ran into this late in the evening, so let me clarify it a bit more.

Contributor

rocksolidwebdesign commented Apr 12, 2012

cool, thanks

Here is the insert statement of the item. It is part of a recursive dup

  SQL (2.1ms)  INSERT INTO "configuration_sets" ("category_id", "created_at", "max", "min", "path", "primary_or_secondary", "source_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["category_id", 7], ["created_at", Thu, 12 Apr 2012 21:50:20 EDT -04:00], ["max", 1], ["min", 1], ["path", []], ["primary_or_secondary", "primary"], ["source_id", 844], ["updated_at", Thu, 12 Apr 2012 21:50:20 EDT -04:00]]

It creates this:

 => #<ConfigurationSet id: 1743, created_at: "2012-04-13 01:50:20", updated_at: "2012-04-13 01:50:20", path: [0], category_id: 7, primary_or_secondary: "primary", min: 1, max: 1, source_id: 844> 

The original is:

#<ConfigurationSet id: 14, created_at: "2011-12-30 22:47:00", updated_at: "2012-04-06 20:22:59", path: [], category_id: 6, primary_or_secondary: "primary", min: 0, max: 0, source_id: 8>

All this is too much detail. The path is serialized and is saved: ["path", []] but the serialization of the empty array produces an empty string.

I'll try to setup a test case for it

Contributor

rocksolidwebdesign commented Apr 13, 2012

hey, I will have a look at this ASAP, thanks

I submitted a pull request with the failing test case. Is easily reproduceable. I looked at your source code, i don't understand why it is breaking though. The fields are not association fields and should be handled by the regular dup which works correctly. Somehow the custom serialization is getting overridden by default serialization.

Contributor

rocksolidwebdesign commented Apr 13, 2012

have you tried using ArrayPack instead of ArrayPack.new ? ... the rails docs say you're supposed to just pass the model, not an instance of the model

This doesn't look to be very well documented. What you are referring to with passing the Class is it will expect what it pulls out of the database to be that Class. I read that Rails 3.1 supported custom serialization because they refactored the serialization, then I just looked for examples. I don't see it in the documentation. This is the closest thing I found: http://stackoverflow.com/questions/8638830/more-complex-activerecord-object-serialization

If you see the pull request it has the syntax, after you run it you can see the sqlite db, the create statements work as expected, but the amoeba duplication does not. Regular duplication does work though.

Maybe it's related to this
rails/rails#5810

Contributor

rocksolidwebdesign commented Apr 13, 2012

So I just implemented spec for plain serialized columns and amoeba has no errors. In addition, the pull request you submitted did not test amoeba, it only tested the original object aka old_post. This means that there is an error with your column serializer and not with amoeba. I'm closing this ticket for now, once you get your custom serializer working outside of amoeba, feel free to open a new ticket if amoeba does in fact break on your custom serializer.

Good luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment