-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Inheritance of serializer #1188
Comments
Try defining Otherwise, we'll need some more information of
|
Also, if you could help improve https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md#how-can-i-help that would be great, too :) (and https://github.com/rails-api/active_model_serializers#getting-help ) |
Thanks @bf4. I could ask this on Stackoverflow. Could you close this issue please? |
@givigier Closing for now, please post the link of the StackOverflow question here if you do open one. |
(And link from your question here, as well.) |
I guess that it's a bug. I'll try to explain exactly what I did. class ApplicationSerializer < ActiveModel::Serializer
def id
object.slug
end
end class V1::CompanySerializer < ApplicationSerializer
attributes :id, :email
end When I run on {"id"=>"company-1", "email"=>"[email protected]"} "company-1" is the slug of company with email [email protected] When I run on {"id"=>"1", "email"=>"[email protected]"} In this case, slug is not setted as id. It also does not work when I try to use concerns like this: module SerializerSluggable
extend ActiveSupport::Concern
def id
object.slug
end
end require 'concerns/serializer_slugglable'
class V1::CompanySerializer < ActiveModel::Serializer
include SerializerSluggable
attributes :id, :email
end I got exactly the same result: When I run on {"id"=>"company-1", "email"=>"[email protected]"} "company-1" is the slug of company with email [email protected] When I run on {"id"=>"1", "email"=>"[email protected]"} My rails version is 4.2.2 and ruby version is 2.2.0. |
what happens if you do: resource = SerializableResource.new(@company, serializer: V1::CompanySerializer)
resource.serializer.id ? |
resource = ActiveModel::SerializableResource.new(@company, serializer: V1::CompanySerializer)
resource.serializer.id return NoMethodError: undefined method `id' for V1::CompanySerializer:Class |
well, that didn't do what I thought it would... anyway, |
I'm guessing the bug is in Serializer._attribute(*args) that conditionally defines attribute methods on the serializer , and that sonething is wrong in one of them What's ApplicationSerializer.instance_method(:id).source_location And CompanySerializer.instance_method(:id).source_location And for each serializer Serializer._attributes On a SerializableResource you can call .serializer |
class Company < ActiveRecord::Base
end Company is an ActiveRecord model. It's not necessary to add read_attribute_for_serialization(attr) or am I wrong? @beauby I added class ApplicationSerializer < ActiveModel::Serializer
def id
object.slug
end
end class V1::CompanySerializer < ApplicationSerializer
attributes :id, :email
end It only works when I'm using |
You're right about this. In order to help you, we still need the output of: ApplicationSerializer.instance_method(:id).source_location
ApplicationSerializer.instance_method(:id).source
CompanySerializer.instance_method(:id).source_location
CompanySerializer.instance_method(:id).source
ApplicationSerializer._attributes
ApplicationSerializer.attributes
CompanySerializer._attributes
CompanySerializer.attributes |
ApplicationSerializer.instance_method(:id).source_location ["/Users/givigier/Sites/agrid-api/app/serializers/application_serializer.rb", 4] ApplicationSerializer.instance_method(:id).source " def id\n object.slug\n end\n" V1::CompanySerializer.instance_method(:id).source_location ["/Users/givigier/Sites/agrid-api/app/serializers/v1/company_serializer.rb", 9] V1::CompanySerializer.instance_method(:id).source " def id\n object.slug\n end\n" ApplicationSerializer._attributes [:id] ApplicationSerializer.attributes [] V1::CompanySerializer._attributes [:id, :email] V1::CompanySerializer.attributes [] |
So what happens is that, in your parent serializer ( |
It fixes the issue, thanks. I was going to try contributing but you fix fastly 👍 |
@beauby Can I close the issue? |
Sure 👍 |
I'm trying to use something like this on branch master AMS but it does not work. When I use CustomerSerializer, it does include the attribute :id. Is it possible to do this on AMS?
The text was updated successfully, but these errors were encountered: