Elixir 长生不老药理解返回一个明星角色'*';
我有一个在Elixir 长生不老药理解返回一个明星角色'*';,elixir,list-comprehension,phoenix-framework,ecto,Elixir,List Comprehension,Phoenix Framework,Ecto,我有一个在p.following中返回的角色模型列表,我想将followind\u id字段从这个模型列表中提取到一个单独的列表中 p.followings returns... [ %Poaster.Personas.Following{ __meta__: #Ecto.Schema.Metadata<:loaded, "followings">, followed: %Poaster.Personas.Persona{ __
p.following
中返回的角色模型列表,我想将followind\u 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]
}
]
但是,当我使用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"
}
]
对于p正如我在评论中提到的,并且正如上所讨论的,您收到的'*'
实际上是您期望的列表:[42]
这是因为42是*
字符的码点(您可以通过在iex会话中执行?*
来验证这一点)。在Elixir和Erlang中,当您有一个整数列表,并且所有整数都是字符的有效代码点时,当您使用IO.inspect
,它将打印字符列表,但它是一个列表,您可以像使用任何列表一样使用它
例如,如果在iex提示符中键入[104、101、108、108、111]
,您将返回'hello'
,但单引号表示它是一个字符表,您可以对其执行任何列表操作。'*'
实际上相当于[42]
,因为42是*
的代码点,当您在Elixir中创建一个包含所有字符代码点的整数的列表时,您可以在尝试打印时打印字符列表,但您应该能够正常使用该列表。尝试执行List。首先对该结果执行,您将看到它生成42。你可能会在这里找到答案……这确实回答了一些问题。谢谢,@sbacarob!如果你在帖子中回答这个问题,我会将其标记为Accepted。另外,请注意,在理解中使用到:
不会影响之前声明的persona_id
变量,因为所有变量在Elixir中都是不可变的。一旦你提到它映射到该字符串,它就完全有意义了,这对我来说只是一个非常意外的行为,所以我从来没有想过。我感谢你的帮助!
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"
}
]