Preload a couple of models to the result of Repo.all

elixir phoenix-framework

792 просмотра

1 ответ

I have a function from a third-party library which returns data from a db:

cars = get_all_cars #third-party function 

Internally it does something like Repo.all(from c in Car). I want to preload 2 models associated with the model Car. I could do this

`Repo.all(from c in Car, preload: [:driver, :driver_to_car])`

but I don't have access and don't want to change the source code of get_all_cars. How can I do this then?

Note that Car and Driver has many to many relationship.

Источник Размещён: 08.11.2019 11:06

Ответы (1)

0 плюса

You can pass a struct or list of structs to Repo.preload/3:

cars = get_all_cars |> Repo.preload([:driver, :driver_to_car])

This would function identical to your example code.

Автор: Dogbert Размещён: 20.08.2016 09:26
Вопросы из категории :