Bitwise
Bitwise

Reputation: 8461

Building a custom query and nested association definitions

I'm trying to preload a has_many association to a passed in struct and the has_many association of the passed on structs association.

Here is what I'm working with right now:

project =
 Repo.get!(Project, id)
 |> Repo.preload([rows: {query, [images: from i in Image, order_by: i.index]}])

But this is returning this error:

This error may happen when you forget a comma in a list or other container:

    [a, b c, d]

Elixir cannot compile otherwise. Syntax error before: ','

This works just fine though:

project =
  Repo.get!(Project, id)
  |> Repo.preload([rows: {query, [:images]}])

But in this query I'm not able to do the ordering I want on images. Can anybody help me with this?

Upvotes: 1

Views: 60

Answers (1)

Dogbert
Dogbert

Reputation: 222040

The expression

[images: from i in Image, order_by: i.index]

is ambiguous because of the comma. It can either be interpreted as:

[images: from(i in Image, order_by: i.index)]

or as:

[images: from(i in Image), order_by: i.index]

You need to add explicit parentheses to resolve the ambiguity. In this case, you want:

[images: from(i in Image, order_by: i.index)]

Upvotes: 5

Related Questions