Elixir 如何在不预加载关联的情况下加载关联?
我有以下两种型号:Elixir 如何在不预加载关联的情况下加载关联?,elixir,ecto,Elixir,Ecto,我有以下两种型号: schema "users" do field :email, :string field :crypted_password, :string field :password, :string, virtual: true has_many :exercise_entries, ExerciseEntry timestamps end 以及: 我在iex中运行了以下代码: u = Repo.get(User, 1) iex(8)
schema "users" do
field :email, :string
field :crypted_password, :string
field :password, :string, virtual: true
has_many :exercise_entries, ExerciseEntry
timestamps
end
以及:
我在iex中运行了以下代码:
u = Repo.get(User, 1)
iex(8)> u.exercise_entries
#Ecto.Association.NotLoaded<association :exercise_entries is not loaded>
u=Repo.get(用户,1)
iex(8)>美国演习项目
#未加载
我知道我可以做
Repo.get(User,1)|>Repo.preload([:exercise\u entries])
然后我可以访问exercise\u条目。不过,是否有可能在不预先加载练习项的情况下以某种方式加载练习项?Ecto需要执行另一个数据库查询以获取练习项。这将是正常的Repo.all
query,但您可以使用exto.assoc
ee = Repo.all(Ecto.assoc(u, :exercise_entries))
ee = Repo.all(Ecto.assoc(u, :exercise_entries))