我有以下两个模型:
schema "users" do
field :email, :string
field :crypted_password, :string
field :password, :string, virtual: true
has_many :exercise_entries, ExerciseEntry
timestamps
end和:
schema "exercise_entries" do
field :date, Ecto.DateTime
field :exercise, :string
field :category, :string
field :weight, :float
field :reps, :integer
belongs_to :user, User
timestamps
end我在iex中运行了以下代码:
u = Repo.get(User, 1)
iex(8)> u.exercise_entries
#Ecto.Association.NotLoaded<association :exercise_entries is not loaded>我知道我可以执行Repo.get(User,1) |> Repo.preload([:exercise_entries]),然后我就可以访问exercise_entries了。但是,有没有可能以某种方式加载exercise_entries而不先预加载它们?
发布于 2016-01-19 06:46:35
Ecto需要执行另一个数据库查询来获取练习条目。这将是一个普通的Repo.all查询,但是您可以使用Ecto.assoc而不是手动构建它
ee = Repo.all(Ecto.assoc(u, :exercise_entries))https://stackoverflow.com/questions/34864508
复制相似问题