Skip to content

Loading…

AR: Serialized field won't be deserialized properly if class is not loaded at the deserialize moment #8596

Closed
phuongnd08 opened this Issue · 4 comments

2 participants

@phuongnd08

I create my custom struct with this:

class MyStruct < Struct.new; end

And serialize it inside another model:

class MyModel < AR::Base
  serialize :data
end

And create a model: MyModel.create(data: MyStruct.new)

When I restart the Rails server, Calling MyModel.first.data return a string (it should return a struct)
It will return an object if I require 'my_struct' beforehand.

@rafaelfranca
Ruby on Rails member

This is how it works. There is no information what class it should require, so you need to explicitly require it.

@rafaelfranca
Ruby on Rails member

If you want to make it work pass MyStruct as second argument of serialize

@phuongnd08

Well, what if I have 10 types of Struct that I want to serialize into that field? then that does mean I have to require 10 times while they are already in rails auto load paths.

@rafaelfranca
Ruby on Rails member

Unfortunately yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.