Include decorated nested objects in Rails 5 & Draper

922 Views Asked by At

I am using Rails 5 and Draper to decorate a model.

My models are posts and comments and there is a decorator for comments.

I want to fetch all posts and eager load the decorated comments.

My starting point is

@posts = Post.all.includes(:comments)

But this of course does not return the decorated comments. What do I have to modify to get the rendered comments?

1

There are 1 best solutions below

0
zwippie On

From the draper README:

Decorating Associated Objects

You can automatically decorate associated models when the primary model is decorated. Assuming an Article model has an associated Author object:

class ArticleDecorator < Draper::Decorator
  decorates_association :author
end

When ArticleDecorator decorates an Article, it will also use AuthorDecorator to decorate the associated Author.