Sorting by a nested struct that is preloaded

时间:2019-01-07 13:58:20

标签: elixir ecto

Is it possible to sort by a column from a nested collection that I fetch using preload in Ecto?

users = MyApp.User.find_by_id(1) |> Repo.preload(locations: [:address])

I want to order by the Location struct field: Location.sort_order

1 个答案:

答案 0 :(得分:2)

预加载接受查询,如文档preload/3中所示。

A <- seq(1900,2000,1)
B <- rnorm(101,10,2)
df <- data.frame(A=A,B=B)

G1 <- c(1963,1982,1952)
G2 <- c(1920,1933,1995)

# This doesn't do what I would like it to achieve
df$group <- ifelse(df$A == G1,"G1",ifelse(df$A == G2,"G2","G0"))

您还可以将comments_query = from c in Comment, order_by: c.published_at Repo.all from p in Post, preload: [comments: ^comments_query] 与子查询(例如片段)一起使用。

join/5

(不确定ecto 2是否支持,但可能会支持,肯定是3)