Select preload-field in Ecto Query

Is it possible to do select on preload in ecto query ?
e.g.

query = from p in Post, preload: [:comments], select: map(p, [:comments, :title])
Repo.all(query)
comments_query =
  from Comment, select: [:data]

query =
  from p in Post,
  preload: [comments: ^comments_query],
  select: map(p, [:comments, :title])

Should do the job.

2 Likes

Does this still work?

I get ** (Tds.Error) Line 1 (Error 207): Invalid column name 'comments'.

I think syntax changed a bit. From docs:

Often times, you may want posts and comments to be selected and filtered in the same query. For such cases, you can explicitly tell an existing join to be preloaded into the result set:

Repo.all from p in Post,
           join: c in assoc(p, :comments),
           where: c.published_at > p.updated_at,
           preload: [comments: c]

So I think you can now add select: [c.data] for the example above.

1 Like

I could not get that to work.

Please show error messages or other information, this sort of comment is not useful as is. You need to show the exact code you are running.

1 Like

I have examples in another post.