-
Static:
User.includes(:posts => :comments)
-
Dynamic:
:posts => :comments
is a product of user's input
-
Let's say you're building a JSON API
-
You want to expose associations for objects
{ "user": { "id": 57, "nickname": "Jankec", ==> "posts": [ { "id": 7, "title": "I didn't know Minitest was fun", ======> "comments": [ {"id": 89, ...}, {"id": 103, ...}, ] } ] } }
-
You can expose each association, but user may not need some of them
-
Lots of associations => Enormous response body
-
You can easily get Stack level too deep errors if you're not careful
class UserMapper < Yaks::Mapper has_many :posts end class PostMapper < Yaks::Mapper has_one :author, mapper: UserMapper end { "user": { "posts": [ { "author": { "posts": [ { "author": { ... } } ] } } ] } }
-
-
It's easiest if clients can just choose which associations they want
-
/posts?include=author
-
/users/1?include=posts.comments
-