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 doesn't work on nested associations in serializer #1972

Closed
dnt294 opened this issue Nov 12, 2016 · 3 comments
Closed

Include doesn't work on nested associations in serializer #1972

dnt294 opened this issue Nov 12, 2016 · 3 comments

Comments

@dnt294
Copy link

dnt294 commented Nov 12, 2016

I've already posted this on Stackoverflow for more help, but I also post it here.


So I have models like this:

  • folder, up_file has 1 chat_room
  • chat_room belongs to crmable with polymorphic: true (so 1 chat_room may belongs to folder or 1 up_file)
  • chat_room has many comments
  • comment belongs to chat_room and user (who posted the comment)

I've defined all relationship like this:

class Folder < ApplicationRecord
    has_one :chat_room, as: :crmable, dependent: :destroy
end

class UpFile < ApplicationRecord
    has_one :chat_room, as: :crmable, dependent: :destroy
end

class ChatRoom < ApplicationRecord
    belongs_to :crmable, polymorphic: true

    has_many :comments, dependent: :destroy
    has_many :users, through: :comments
end

class Comment < ApplicationRecord
    belongs_to :chat_room
    belongs_to :user
end

I've follow their tutorial here but my associations not rendered.

class FolderSerializer < ActiveModel::Serializer
    
    attributes :id, :name, :ancestry, :creator_name #... my other attributes
    
    has_one :chat_room, include: [ :id, comments: :user ]
    # ^ I tried to serialize folder's chat_room with all it comments, along with these comments' users.
end

All I got is this - chat_room association not serialized?

enter image description here

@beauby
Copy link
Contributor

beauby commented Nov 12, 2016

Could you point me to where it says in the docs that this is supported? (serializer-level include directives)

@NullVoxPopuli
Copy link
Contributor

per that PR's description:

Note: the serializer-level include option has been dropped for now (i.e. doing has_many :comments, include: 'author, posts' is still not possible).

@NullVoxPopuli
Copy link
Contributor

it's highly recommended to use render option's includes.

render json: @resource, include: 'author,posts'

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