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

Include does not work on nested associations #1194

Closed
givigier opened this issue Sep 23, 2015 · 6 comments
Closed

Include does not work on nested associations #1194

givigier opened this issue Sep 23, 2015 · 6 comments

Comments

@givigier
Copy link

I'm trying to use the feature of nested associations. My structure of serializers is:

class V1::QuotesController < ApplicationController
  def show
    render json: @quote, serializer: V1::QuoteSerializer, include: { company: [:addresses, :phones] }
  end
end
class V1::QuoteSerializer < ActiveModel::Serializer
  attributes :id
  has_one :company, serializer: V1::CompanySerializer
end
class V1::CompanySerializer < ActiveModel::Serializer
  attributes :id
  has_many :phones, serializer: V1::PhoneSerializer
  has_many :addresses, serializer: V1::AddressSerializer
end
class V1::AddressSerializer < ActiveModel::Serializer
  attributes :city
end
class V1::PhoneSerializer < ActiveModel::Serializer
  attributes :number
end

When I add 'include' of associations on controller(like on QuotesController) it works fine and return:
{"id"=>"1", "company"=>{"id"=>"1", "addresses"=>[], "phones"=>[]}}

Otherwise, when I add 'include' of associations on serializer it does not work and doesn't return addresses and phones.

class V1::QuotesController < ApplicationController
  def show
    render json: @quote, serializer: V1::QuoteSerializer
  end
end
class V1::QuoteSerializer < ActiveModel::Serializer
  has_one :company, serializer: V1::CompanySerializer, include: [:addresses, :phones]
end

{"id"=>"1", "company"=>{"id"=>"1"}}

I was reading the docs and apparently it should work on serializer too.

@NullVoxPopuli
Copy link
Contributor

It no longer is implemented on serializer association definitions. This was
a design decision. But an argument could be made for bringing it back.
Like, if you always want a few nested associations on a particular object.

@beauby
Copy link
Contributor

beauby commented Sep 23, 2015

As @NullVoxPopuli said, this feature is currently not part of master (only the adapter option). Could you provide a link to the doc where it says otherwise so that we correct it?

@givigier
Copy link
Author

I was reading on #1127. It's not a doc, sorry.

I understand the point that you said but this logic on controller is not against DRY? I'll need to set include on every controller that I use the serializer. If this logic was on serializer I didn't repeat on every controller.

@beauby
Copy link
Contributor

beauby commented Sep 23, 2015

@givigier Currently, we're thinking about going down the way described in #1193. Basically, what it allows, in your case, is:

class V1::QuoteSerializer < ActiveModel::Serializer
  attributes :id
  has_one :company do
    attributes :id
    has_many :phones do
      attributes :number
    end
    has_many :addresses do
      attributes :city
    end
  end
end

Combined with render json: @quotes, include: '**', it will have the desired result. Feel free to comment/add suggestions to #1090 (which is where we discuss how nested serializers should be).

@beauby
Copy link
Contributor

beauby commented Sep 23, 2015

Thanks for pointing out the inconsistent PR description though, I'm fixing this right now.

@givigier
Copy link
Author

Thanks! I guess we could close this issue.

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

3 participants