trailblazer / representable

Maps representation documents from and to Ruby objects. Includes JSON, XML and YAML support, plain properties and compositions.
http://trailblazer.to/2.1/docs/representable.html
MIT License
689 stars 108 forks source link

Error while starting server with Rails 5.2.0.rc1 #224

Closed unrooty closed 6 years ago

unrooty commented 6 years ago

I updated to Rails 5.2.0.rc1 and now have such error: activerecord-5.2.0.rc1/lib/active_record/coders/yaml_column.rb:24:in 'load': undefined method 'new' for Representable::JSON:Module (NoMethodError)

Can anybody help me with this issue?

apotonick commented 6 years ago

Looks like Rails is grabbing the wrong constant - not in Representable's hand. Maybe dig into the yaml_column code?

unrooty commented 6 years ago

This is the code of yaml_column.rb 'load' method:

def load(yaml) return object_class.new if object_class != Object && yaml.nil? return yaml unless yaml.is_a?(String) && /^---/.match?(yaml) obj = YAML.load(yaml) assert_valid_value(obj, action: "load") obj ||= object_class.new if object_class != Object obj end

I think it tries to create an instance of module and throws exception while this process. But I don't understand why it tries to create instance of JSON module in yaml_column.

apotonick commented 6 years ago

Yepp, must be somewhere further up the stack. Is Rails maybe looking for constants that equal JSON or something nasty like that? :thinking:

renchap commented 6 years ago

Could it be here: https://github.com/rails/rails/blob/master/activestorage/app/models/active_storage/blob.rb#L22 ? This is the first use of JSON when going up the stack.

renchap commented 6 years ago

This is the culprit, replacing JSON with ActiveRecord::Coders::JSON fixes the issue. I will submit a PR to Rails.

unrooty commented 6 years ago

@renchap thank for work. Waitng for megre.

renchap commented 6 years ago

It has been merged into the 5-2-stable branch. You can switch rails to this branch in your Gemfile to get the fix, or wait for the next Rails 5.2 version to be released.