중첩된 includes 사용

Tags:

association이 중첩되거나(polymorphic) 둘 이상의 includes를 사용하고 싶을 때가 있다. 댓글과 대댓글의 관계가 있고, 댓글이 대댓글을 보거나 부모의 댓글을 참조하거나, 사용자 모델을 참조할때 includes 이렇게 활용할 수 있다.

아래와 같은 관계일 때

Comment - commentable - comment
        - user

comment는 comments를 가지고 있고, 사용자에 접근가능하고 부모 댓글(commentable)에 접근할때, comment의 comments 역시 같은 접근을 할때 아래와 같이 중첩된 includes를 사용할 수 있다.

comments.includes({comments: [:comments, :user, :commentable]}, :user)

참조링크