本文介绍了预载所有关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此类似的ERM:

I have an ERM similar to this one:

--------     --------     --------
|ModelA|-----|ModelB|-----|ModelC|
--------     --------     --------

我得到具有以下内容的ModelA及其ModelB:

I get ModelA and its ModelBs with the following:

modela = Repo.get(ModelA,1)
modela = preload(modela, :modelb)

现在,我可以使用modela.modelb访问ModelB.但是,我又如何预加载ModelC?当我打印我的模型A时,它说没有加载模型c.

Now I can access ModelBs with modela.modelb. But how can I preload ModelC aswell? When I print my modelA, it says, that modelc is not loaded.

推荐答案

尚不能评论,但您必须在ModelA中定义嵌套关系.请参见 Ecto.Schema

Can't comment yet but you have to define the nested relationship in ModelA. See Ecto.Schema

has_one :model_c, through: [:model_b, :model_c]

has_one :model_c, through: [:model_b, :model_c]

这篇关于预载所有关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 05:00