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

Serializer fails when used with class extending ActiveModel::Model #520

Closed
chriskilding opened this issue Feb 24, 2014 · 5 comments
Closed

Comments

@chriskilding
Copy link

When I use AMS in my project, all 'real' AR models work fine with the serialisers, but there are 2 problems using it with a virtual model that inherits from ActiveModel::Model:

  1. Controller does not use the AMS serialiser unless explicitly specified
    I have to specify it with render json: foo, serializer: FooSerializer... to make this work.
  2. After serialiser is explicitly defined, using it fails with the following trace:
NoMethodError:
       undefined method `read_attribute_for_serialization' for #<Foo:0xblah>
     # (eval):3:in `_fast_attributes'
     # <controller that called `render json: foo...`>  

Using AMS v0.8.1 on Rails 4.0.2 / Jruby 1.7.9

Is this just a bug in the way I am using AMS, or does it not yet support virtual models?

@arthurnn
Copy link
Contributor

You active model subclass needs to include this module, https://github.com/rails/rails/blob/c4fb191a4cc246db849f7acf31bd7edb4664cca5/activemodel/lib/active_model/serialization.rb , so probably adding a include ActiveModel::Serialization, will fix the issue.

@chriskilding
Copy link
Author

That did the trick - could this be documented on the wiki / README?

Generally the assumption when you include ActiveModel::Model is that you get everything from AM, including serialisation support (partially true - you get as_json and to_json support, it just doesn't work with the AMS serialiser class), hence my mistake above, so it would be wise to warn other AM:M users about this.

@steveklabnik
Copy link
Contributor

It's in the README.

@ZiaMM
Copy link

ZiaMM commented Mar 20, 2016

In version 0.09, I added the above Same error. Tried this line too:
include ActiveModel::Serializers::JSON
... same error.
Moved to Version 0.10 rc4, and now either of these seem to work. Sill doesn't work - likely due to different syntax needed for this version - but I can make this error appear and disappear with either of the above "include" options.

@bf4
Copy link
Member

bf4 commented Mar 20, 2016

@ZiaMM Please open a new issue (and check the docs)

@rails-api rails-api locked and limited conversation to collaborators Mar 20, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants