select_merge
works very well for virtual attributes:
from u in User, select_merge: %{virtual_field: true}
However working with associations it is not that easy. The only solution I found is something to this:
from p in Post,
join u in assoc(p, :author),
select_merge: %{author: %{
id: u.id,
name: u.name,
...
virtual_field: true,
} }
So I have to write every field in each query, not very practical IMHO. I’m not sure how to do the same when the has_many
association either:
from u in User,
join: p in assoc(u, :posts),
select_merge: %{posts: ??? }
And working with inner relations can be really hairy:
from u in User,
join: p in assoc(u, :posts),
join: i in assoc(p, :images),
select_merge: # virtual field for images
virtual_field: true
is a simple example, usually I work with other joins to build this virtual field —i.e. liked
field if the current user likes or not the posts.
So my question: Is there an easier way to work with those virtual fields for “inner” associations??