Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize attributes which is used serialize in active model definition #126

Open
danielmalaton opened this issue Dec 17, 2022 · 0 comments

Comments

@danielmalaton
Copy link
Contributor

danielmalaton commented Dec 17, 2022

Context:
Suppose I have User model like this:

class Rot13JSON
  def self.rot13(string)
    string.tr("a-zA-Z", "n-za-mN-ZA-M")
  end

  # Serializes an attribute value to a string that will be stored in the database.
  def self.dump(value)
    rot13(ActiveSupport::JSON.dump(value))
  end

  # Deserializes a string from the database to an attribute value.
  def self.load(string)
    ActiveSupport::JSON.load(rot13(string))
  end
end

class User < ActiveRecord::Base
  serialize :preferences, Rot13JSON
end

and UserSerializer class definition:

class UserSerializer < Panko::Serializer
  attributes :preferences
end

When serializing user instances:

# create user with non-nil preferences
user = User.create!(preferences: {...})

# serialize
UserSerializer.new.serialize(user) # { preferences: nil }

Expected

{ "preferences": {...} }

Got

{ "preferences": null }

Question:
Will you have the plan to fix it soon? I'm migrating from AMS to Panko and getting in trouble with it

Reference:
Serialize function in Active model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant