我有一个返回的角色模型列表p.followings
我想提取followed_id
将此模型列表中的字段放入单独的列表中。
p.followings
returns...
[
%Poaster.Personas.Following{
__meta__: #Ecto.Schema.Metadata<:loaded, "followings">,
followed: %Poaster.Personas.Persona{
__meta__: #Ecto.Schema.Metadata<:loaded, "personas">,
background_image_url: nil,
bio: "ASDF",
followings: #Ecto.Association.NotLoaded<association :followings is not loaded>,
id: 42,
inserted_at: ~N[2020-08-14 01:52:17],
name: nil,
profile_image_url: nil,
updated_at: ~N[2020-08-14 16:19:56],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 1,
username: "test"
},
followed_id: 42,
id: 1,
inserted_at: ~N[2020-08-12 20:35:09],
persona: #Ecto.Association.NotLoaded<association :persona is not loaded>,
persona_id: 1,
updated_at: ~N[2020-08-12 20:35:09]
}
]
我只想在此处获取 follow_id 的列表,以便我可以进行查询以获取我关注的角色的帖子列表。
我想找回类似的东西[42]
.
当我做Enum.map(ps.followings, fn follow -> follow.followed_id end)
,这是我期望能够运行来获得这个,我只是回到控制台'*'
当我尝试使用理解时into
选项,进入一个空列表,这也是我得到的。
persona_ids = []
for p <- p.followings, into: persona_ids, do: p.followed_id
IO.inspect(persona_ids)
[]
但是,当我运行上述理解时p.followed
,它返回角色列表:
for p <- p.followings, into: persona_ids, do: p.followed
[
%Poaster.Personas.Persona{
__meta__: #Ecto.Schema.Metadata<:loaded, "personas">,
background_image_url: nil,
bio: "ASDF",
followings: #Ecto.Association.NotLoaded<association :followings is not loaded>,
id: 42,
inserted_at: ~N[2020-08-14 01:52:17],
name: nil,
profile_image_url: nil,
updated_at: ~N[2020-08-14 16:19:56],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 1,
username: "test"
}
]
我需要 ID 列表,而不是角色模型列表,以便我可以进行适当的 Ecto 查询以获取我关注的角色的帖子。
这里发生了什么?我究竟做错了什么?有更好的方法来做我想做的事情吗?